94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
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 TooManyRequestsHttpError extends BadRequestError {
|
|
constructor(cause?: Error) {
|
|
super(
|
|
`You're making too many requests!`,
|
|
`We need some rest.`,
|
|
'',
|
|
cause
|
|
);
|
|
}
|
|
|
|
get errorCode(): number {
|
|
return 429;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |