1+ import type { ErrorResponse , HttpStatusCode } from '../types/rest.js' ;
2+ import { HttpErrorCodes } from './constants.js' ;
3+
14export class RouteMatchingError extends Error {
25 constructor (
36 message : string ,
@@ -15,3 +18,145 @@ export class ParameterValidationError extends RouteMatchingError {
1518 this . name = 'ParameterValidationError' ;
1619 }
1720}
21+
22+ abstract class ServiceError extends Error {
23+ abstract readonly statusCode : HttpStatusCode ;
24+ abstract readonly errorType : string ;
25+ public readonly details ?: Record < string , unknown > ;
26+
27+ constructor (
28+ message ?: string ,
29+ options ?: ErrorOptions ,
30+ details ?: Record < string , unknown >
31+ ) {
32+ super ( message , options ) ;
33+ this . name = 'ServiceError' ;
34+ this . details = details ;
35+ }
36+
37+ toJSON ( ) : ErrorResponse {
38+ return {
39+ statusCode : this . statusCode ,
40+ error : this . errorType ,
41+ message : this . message ,
42+ ...( this . details && { details : this . details } ) ,
43+ } ;
44+ }
45+ }
46+
47+ export class BadRequestError extends ServiceError {
48+ readonly statusCode = HttpErrorCodes . BAD_REQUEST ;
49+ readonly errorType = 'BadRequestError' ;
50+
51+ constructor (
52+ message ?: string ,
53+ options ?: ErrorOptions ,
54+ details ?: Record < string , unknown >
55+ ) {
56+ super ( message , options , details ) ;
57+ }
58+ }
59+
60+ export class UnauthorizedError extends ServiceError {
61+ readonly statusCode = HttpErrorCodes . UNAUTHORIZED ;
62+ readonly errorType = 'UnauthorizedError' ;
63+
64+ constructor (
65+ message ?: string ,
66+ options ?: ErrorOptions ,
67+ details ?: Record < string , unknown >
68+ ) {
69+ super ( message , options , details ) ;
70+ }
71+ }
72+
73+ export class ForbiddenError extends ServiceError {
74+ readonly statusCode = HttpErrorCodes . FORBIDDEN ;
75+ readonly errorType = 'ForbiddenError' ;
76+
77+ constructor (
78+ message ?: string ,
79+ options ?: ErrorOptions ,
80+ details ?: Record < string , unknown >
81+ ) {
82+ super ( message , options , details ) ;
83+ }
84+ }
85+
86+ export class NotFoundError extends ServiceError {
87+ readonly statusCode = HttpErrorCodes . NOT_FOUND ;
88+ readonly errorType = 'NotFoundError' ;
89+
90+ constructor (
91+ message ?: string ,
92+ options ?: ErrorOptions ,
93+ details ?: Record < string , unknown >
94+ ) {
95+ super ( message , options , details ) ;
96+ }
97+ }
98+
99+ export class MethodNotAllowedError extends ServiceError {
100+ readonly statusCode = HttpErrorCodes . METHOD_NOT_ALLOWED ;
101+ readonly errorType = 'MethodNotAllowedError' ;
102+
103+ constructor (
104+ message ?: string ,
105+ options ?: ErrorOptions ,
106+ details ?: Record < string , unknown >
107+ ) {
108+ super ( message , options , details ) ;
109+ }
110+ }
111+
112+ export class RequestTimeoutError extends ServiceError {
113+ readonly statusCode = HttpErrorCodes . REQUEST_TIMEOUT ;
114+ readonly errorType = 'RequestTimeoutError' ;
115+
116+ constructor (
117+ message ?: string ,
118+ options ?: ErrorOptions ,
119+ details ?: Record < string , unknown >
120+ ) {
121+ super ( message , options , details ) ;
122+ }
123+ }
124+
125+ export class RequestEntityTooLargeError extends ServiceError {
126+ readonly statusCode = HttpErrorCodes . REQUEST_ENTITY_TOO_LARGE ;
127+ readonly errorType = 'RequestEntityTooLargeError' ;
128+
129+ constructor (
130+ message ?: string ,
131+ options ?: ErrorOptions ,
132+ details ?: Record < string , unknown >
133+ ) {
134+ super ( message , options , details ) ;
135+ }
136+ }
137+
138+ export class InternalServerError extends ServiceError {
139+ readonly statusCode = HttpErrorCodes . INTERNAL_SERVER_ERROR ;
140+ readonly errorType = 'InternalServerError' ;
141+
142+ constructor (
143+ message ?: string ,
144+ options ?: ErrorOptions ,
145+ details ?: Record < string , unknown >
146+ ) {
147+ super ( message , options , details ) ;
148+ }
149+ }
150+
151+ export class ServiceUnavailableError extends ServiceError {
152+ readonly statusCode = HttpErrorCodes . SERVICE_UNAVAILABLE ;
153+ readonly errorType = 'ServiceUnavailableError' ;
154+
155+ constructor (
156+ message ?: string ,
157+ options ?: ErrorOptions ,
158+ details ?: Record < string , unknown >
159+ ) {
160+ super ( message , options , details ) ;
161+ }
162+ }
0 commit comments