swaf/src/HttpError.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import {WrappingError} from "./Utils";
export abstract class HttpError extends WrappingError {
public readonly instructions: string;
constructor(message: string, instructions: string, cause?: Error) {
super(message, cause);
this.instructions = instructions;
}
get name(): string {
return this.constructor.name;
}
abstract get errorCode(): number;
}
export class BadRequestError extends HttpError {
public readonly url: string;
constructor(message: string, instructions: string, url: string, cause?: Error) {
super(message, instructions, cause);
this.url = url;
}
get errorCode(): number {
return 400;
}
}
export class ForbiddenHttpError extends BadRequestError {
constructor(thing: string, url: string, cause?: Error) {
super(
`You don't have access to this ${thing}.`,
`${url} doesn't belong to *you*.`,
url,
cause
);
}
get errorCode(): number {
return 403;
}
}
export class NotFoundHttpError extends BadRequestError {
constructor(thing: string, url: string, cause?: Error) {
super(
`${thing.charAt(0).toUpperCase()}${thing.substr(1)} not found.`,
`${url} doesn't exist or was deleted.`,
url,
cause
);
}
get errorCode(): number {
return 404;
}
}
export class ServerError extends HttpError {
constructor(message: string, cause?: Error) {
super(message, `Maybe you should contact us; see instructions below.`, cause);
}
get errorCode(): number {
return 500;
}
}
export class ServiceUnavailableHttpError extends ServerError {
constructor(message: string, cause?: Error) {
super(message, cause);
}
get errorCode(): number {
return 503;
}
}