-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
288 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Elysia, t } from 'elysia' | ||
|
||
export class HttpError extends Error { | ||
public constructor( | ||
public message: string, | ||
public status: number, | ||
public cause: string, | ||
public data: any = undefined, | ||
) { | ||
super(message, data) | ||
} | ||
|
||
public static BadRequest(message: string, data?: any) { | ||
return new HttpError('Bad Request', 400, message, data) | ||
} | ||
|
||
public static Unauthorized(message: string, data?: any) { | ||
return new HttpError('Unauthorized', 401, message, data) | ||
} | ||
|
||
public static PaymentRequired(message: string, data?: any) { | ||
return new HttpError('Payment Required', 402, message, data) | ||
} | ||
|
||
public static Forbidden(message: string, data?: any) { | ||
return new HttpError('Forbidden', 403, message, data) | ||
} | ||
|
||
public static NotFound(message: string, data?: any) { | ||
return new HttpError('Not Found', 404, message, data) | ||
} | ||
|
||
public static MethodNotAllowed(message: string, data?: any) { | ||
return new HttpError('Method Not Allowed', 405, message, data) | ||
} | ||
|
||
public static Conflict(message: string, data?: any) { | ||
return new HttpError('Conflict', 409, message, data) | ||
} | ||
|
||
public static UnsupportedMediaType(message: string, data?: any) { | ||
return new HttpError('Unsupported Media Type', 415, message, data) | ||
} | ||
|
||
public static IAmATeapot(message: string, data?: any) { | ||
return new HttpError('I Am A Teapot', 418, message, data) | ||
} | ||
|
||
public static TooManyRequests(message: string, data?: any) { | ||
return new HttpError('Too Many Requests', 429, message, data) | ||
} | ||
|
||
public static InternalServer(message: string, data?: any) { | ||
return new HttpError('Internal Server Error', 500, message, data) | ||
} | ||
|
||
public static NotImplemented(message: string, data?: any) { | ||
return new HttpError('Not Implemented', 501, message, data) | ||
} | ||
|
||
public static BadGateway(message: string, data?: any) { | ||
return new HttpError('Bad Gateway', 502, message, data) | ||
} | ||
|
||
public static ServiceUnavailable(message: string, data?: any) { | ||
return new HttpError('Service Unavailable', 503, message, data) | ||
} | ||
|
||
public static GatewayTimeout(message: string, data?: any) { | ||
return new HttpError('Gateway Timeout', 504, message, data) | ||
} | ||
} | ||
|
||
export function httpError() { | ||
return new Elysia({ name: 'http-error' }) | ||
.decorate('HttpError', HttpError) | ||
.error({ HTTP_ERROR: HttpError }) | ||
.model({ | ||
error: t.Object({ | ||
code: t.String(), | ||
status: t.Number(), | ||
message: t.String(), | ||
data: t.Optional(t.Any()), | ||
}, { | ||
examples: [{ | ||
code: 'Bad Request', | ||
status: 400, | ||
message: 'The request was invalid', | ||
}], | ||
$id: 'GenericError', | ||
}), | ||
}) | ||
.onError({ as: 'global' }, ({ code, error, set }) => { | ||
if (code === 'HTTP_ERROR') { | ||
set.status = error.status | ||
return { | ||
code: error.message, | ||
status: error.status, | ||
message: error.cause, | ||
data: error.data, | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.