Skip to content

Commit 4193b41

Browse files
committed
Create UnauthorizedError
1 parent 5076f48 commit 4193b41

4 files changed

Lines changed: 46 additions & 4 deletions

File tree

core/loadPermissions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import path from 'node:path';
99
import { console } from '../lib/Cluster.js';
1010
import Response from '../lib/Response.js';
1111
import SaplingError from '../lib/SaplingError.js';
12+
import UnauthorizedError from '../lib/UnauthorizedError.js';
1213

1314

1415
/**
@@ -96,7 +97,7 @@ export default async function loadPermissions(next) {
9697
if (request.permission.redirect) {
9798
response.redirect(request.permission.redirect);
9899
} else {
99-
return new Response(this, request, response, new SaplingError('You do not have permission to complete this action.'));
100+
return new Response(this, request, response, new UnauthorizedError());
100101
}
101102
} else {
102103
next();

lib/Response.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { console } from './Cluster.js';
1313
import Redirect from './Redirect.js';
1414
import Templating from './Templating.js';
1515
import Utils from './Utils.js';
16+
import UnauthorizedError from './UnauthorizedError.js';
1617

1718

1819
/**
@@ -43,7 +44,11 @@ export default class Response {
4344
/* Respond based on the given parameters */
4445
if (this.response) {
4546
if (this.error) {
46-
this.errorResponse();
47+
if (this.error instanceof UnauthorizedError) {
48+
this.notFoundResponse();
49+
} else {
50+
this.errorResponse();
51+
}
4752
} else if (content === false) {
4853
this.notFoundResponse();
4954
} else if (typeof content === 'string') {
@@ -239,7 +244,11 @@ export default class Response {
239244
*/
240245
async notFoundResponse() {
241246
/* Log to the server */
242-
console.error('Not Found:', this.request.method && this.request.method.toUpperCase(), this.request.url);
247+
if (this.error instanceof UnauthorizedError) {
248+
console.error('Not Authorized:', this.request.method && this.request.method.toUpperCase(), this.request.url);
249+
} else {
250+
console.error('Not Found:', this.request.method && this.request.method.toUpperCase(), this.request.url);
251+
}
243252

244253
/* Respond with the error */
245254
if (this.ajax === false) {

lib/UnauthorizedError.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* SaplingError
3+
*
4+
* Uniform error handling
5+
*/
6+
7+
import SaplingError from './SaplingError.js';
8+
9+
10+
/**
11+
* The UnauthorizedError class
12+
*/
13+
export default class UnauthorizedError extends SaplingError {
14+
/**
15+
* Initialise the UnauthorizedError class
16+
*/
17+
constructor(...parameters) {
18+
super(...parameters);
19+
20+
this.name = 'UnauthorizedError';
21+
22+
/* Create empty error structure */
23+
this.json = {
24+
errors: [
25+
{
26+
title: 'Unauthorized',
27+
},
28+
],
29+
};
30+
}
31+
}

test/core/loadPermissions.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Request from '../../lib/Request.js';
77
import Response from '../../lib/Response.js';
88
import SaplingError from '../../lib/SaplingError.js';
99
import Storage from '../../lib/Storage.js';
10+
import UnauthorizedError from '../../lib/UnauthorizedError.js';
1011
import User from '../../lib/User.js';
1112
import parseMethodRouteKey from '../../core/parseMethodRouteKey.js';
1213

@@ -101,7 +102,7 @@ test.serial('creates middleware', async t => {
101102
const response = handler.call(t.context.app, t.context.request, t.context.response, () => true);
102103

103104
t.true(response instanceof Response);
104-
t.is(response.error.message, 'You do not have permission to complete this action.');
105+
t.true(response.error instanceof UnauthorizedError);
105106
},
106107

107108
/* Creates post middleware that allows it continue when authorised */

0 commit comments

Comments
 (0)