mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add custom errors
This commit is contained in:
19
src/util/errors/HttpError.ts
Normal file
19
src/util/errors/HttpError.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* An abstract class for all errors that could be thrown by Solid.
|
||||
* All errors inheriting from this should fix the status code thereby hiding the HTTP internals from other components.
|
||||
*/
|
||||
export abstract class HttpError extends Error {
|
||||
public statusCode: number;
|
||||
|
||||
/**
|
||||
* Creates a new HTTP error. Subclasses should call this with their fixed status code.
|
||||
* @param statusCode - HTTP status code needed for the HTTP response.
|
||||
* @param name - Error name. Useful for logging and stack tracing.
|
||||
* @param message - Message to be thrown.
|
||||
*/
|
||||
protected constructor (statusCode: number, name: string, message: string) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
15
src/util/errors/UnsupportedHttpError.ts
Normal file
15
src/util/errors/UnsupportedHttpError.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { HttpError } from './HttpError';
|
||||
|
||||
/**
|
||||
* An error thrown when incoming data is not supported.
|
||||
* Probably because an {@link AsyncHandler} returns false on the canHandle call.
|
||||
*/
|
||||
export class UnsupportedHttpError extends HttpError {
|
||||
/**
|
||||
* Default message is 'The given input is not supported by the server configuration.'.
|
||||
* @param message - Optional, more specific, message.
|
||||
*/
|
||||
public constructor (message?: string) {
|
||||
super(400, 'UnsupportedHttpError', message || 'The given input is not supported by the server configuration.');
|
||||
}
|
||||
}
|
||||
17
test/unit/util/errors/UnsupportedHttpError.test.ts
Normal file
17
test/unit/util/errors/UnsupportedHttpError.test.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||
|
||||
describe('An UnsupportedHttpError', (): void => {
|
||||
it('has status code 400.', async (): Promise<void> => {
|
||||
const error = new UnsupportedHttpError('test');
|
||||
|
||||
expect(error.statusCode).toEqual(400);
|
||||
expect(error.message).toEqual('test');
|
||||
expect(error.name).toEqual('UnsupportedHttpError');
|
||||
});
|
||||
|
||||
it('has a default message if none was provided.', async (): Promise<void> => {
|
||||
const error = new UnsupportedHttpError();
|
||||
|
||||
expect(error.message).toEqual('The given input is not supported by the server configuration.');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user