Skip to content

Commit 9b9ae6e

Browse files
committed
add all http error codes
1 parent 47d7c7d commit 9b9ae6e

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

packages/event-handler/src/rest/constants.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,77 @@ export const HttpVerbs = {
1111
} as const;
1212

1313
export const HttpErrorCodes = {
14+
// 1xx Informational
15+
CONTINUE: 100,
16+
SWITCHING_PROTOCOLS: 101,
17+
PROCESSING: 102,
18+
EARLY_HINTS: 103,
19+
20+
// 2xx Success
21+
OK: 200,
22+
CREATED: 201,
23+
ACCEPTED: 202,
24+
NON_AUTHORITATIVE_INFORMATION: 203,
25+
NO_CONTENT: 204,
26+
RESET_CONTENT: 205,
27+
PARTIAL_CONTENT: 206,
28+
MULTI_STATUS: 207,
29+
ALREADY_REPORTED: 208,
30+
IM_USED: 226,
31+
32+
// 3xx Redirection
33+
MULTIPLE_CHOICES: 300,
34+
MOVED_PERMANENTLY: 301,
35+
FOUND: 302,
36+
SEE_OTHER: 303,
37+
NOT_MODIFIED: 304,
38+
USE_PROXY: 305,
39+
TEMPORARY_REDIRECT: 307,
40+
PERMANENT_REDIRECT: 308,
41+
42+
// 4xx Client Error
1443
BAD_REQUEST: 400,
1544
UNAUTHORIZED: 401,
45+
PAYMENT_REQUIRED: 402,
1646
FORBIDDEN: 403,
1747
NOT_FOUND: 404,
1848
METHOD_NOT_ALLOWED: 405,
49+
NOT_ACCEPTABLE: 406,
50+
PROXY_AUTHENTICATION_REQUIRED: 407,
1951
REQUEST_TIMEOUT: 408,
52+
CONFLICT: 409,
53+
GONE: 410,
54+
LENGTH_REQUIRED: 411,
55+
PRECONDITION_FAILED: 412,
2056
REQUEST_ENTITY_TOO_LARGE: 413,
57+
REQUEST_URI_TOO_LONG: 414,
58+
UNSUPPORTED_MEDIA_TYPE: 415,
59+
REQUESTED_RANGE_NOT_SATISFIABLE: 416,
60+
EXPECTATION_FAILED: 417,
61+
IM_A_TEAPOT: 418,
62+
MISDIRECTED_REQUEST: 421,
63+
UNPROCESSABLE_ENTITY: 422,
64+
LOCKED: 423,
65+
FAILED_DEPENDENCY: 424,
66+
TOO_EARLY: 425,
67+
UPGRADE_REQUIRED: 426,
68+
PRECONDITION_REQUIRED: 428,
69+
TOO_MANY_REQUESTS: 429,
70+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
71+
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
72+
73+
// 5xx Server Error
2174
INTERNAL_SERVER_ERROR: 500,
75+
NOT_IMPLEMENTED: 501,
76+
BAD_GATEWAY: 502,
2277
SERVICE_UNAVAILABLE: 503,
78+
GATEWAY_TIMEOUT: 504,
79+
HTTP_VERSION_NOT_SUPPORTED: 505,
80+
VARIANT_ALSO_NEGOTIATES: 506,
81+
INSUFFICIENT_STORAGE: 507,
82+
LOOP_DETECTED: 508,
83+
NOT_EXTENDED: 510,
84+
NETWORK_AUTHENTICATION_REQUIRED: 511,
2385
} as const;
2486

2587
export const PARAM_PATTERN = /:([a-zA-Z_]\w*)(?=\/|$)/g;

packages/event-handler/src/types/rest.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
22
import type { BaseRouter } from '../rest/BaseRouter.js';
3-
import type { HttpVerbs } from '../rest/constants.js';
3+
import type { HttpErrorCodes, HttpVerbs } from '../rest/constants.js';
44
import type { Route } from '../rest/Route.js';
55

66
type ErrorResponse = {
7-
statusCode: number;
7+
statusCode: HttpStatusCode;
88
error: string;
99
message: string;
1010
};
1111

12+
interface ErrorContext {
13+
path: string;
14+
method: string;
15+
headers: Record<string, string>;
16+
timestamp: string;
17+
requestId?: string;
18+
}
19+
20+
type ErrorHandler<T extends Error = Error> = (
21+
error: T,
22+
context?: ErrorContext
23+
) => ErrorResponse;
24+
25+
interface ErrorConstructor<T extends Error = Error> {
26+
new (...args: any[]): T;
27+
prototype: T;
28+
}
29+
1230
/**
1331
* Options for the {@link BaseRouter} class
1432
*/
@@ -35,6 +53,8 @@ type RouteHandler<T = any, R = any> = (...args: T[]) => R;
3553

3654
type HttpMethod = keyof typeof HttpVerbs;
3755

56+
type HttpStatusCode = (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes];
57+
3858
type Path = `/${string}`;
3959

4060
type RouteHandlerOptions = {
@@ -66,6 +86,8 @@ export type {
6686
CompiledRoute,
6787
DynamicRoute,
6888
ErrorResponse,
89+
ErrorConstructor,
90+
ErrorHandler,
6991
HttpMethod,
7092
Path,
7193
RouterOptions,

0 commit comments

Comments
 (0)