Skip to content

Commit e53aa88

Browse files
authored
improv(event-handler): rename HttpErrorCodes to HttpStatusCodes (#4543)
1 parent 3d1255c commit e53aa88

18 files changed

+211
-194
lines changed

examples/snippets/event-handler/rest/gettingStarted_error_handling.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare function getTodoById<T>(todoId: unknown): Promise<{ id: string } & T>;
22
declare class GetTodoError extends Error {}
33

44
import {
5-
HttpErrorCodes,
5+
HttpStatusCodes,
66
Router,
77
} from '@aws-lambda-powertools/event-handler/experimental-rest';
88
import { Logger } from '@aws-lambda-powertools/logger';
@@ -15,7 +15,7 @@ app.errorHandler(GetTodoError, async (error, reqCtx) => {
1515
logger.error('Unable to get todo', { error });
1616

1717
return {
18-
statusCode: HttpErrorCodes.BAD_REQUEST,
18+
statusCode: HttpStatusCodes.BAD_REQUEST,
1919
message: `Bad request: ${error.message} - ${reqCtx.request.headers.get('x-correlation-id')}`,
2020
error: 'BadRequest',
2121
};

examples/snippets/event-handler/rest/gettingStarted_handle_not_found.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
HttpErrorCodes,
2+
HttpStatusCodes,
33
Router,
44
} from '@aws-lambda-powertools/event-handler/experimental-rest';
55
import { Logger } from '@aws-lambda-powertools/logger';
@@ -12,7 +12,7 @@ app.notFound(async (error, reqCtx) => {
1212
logger.error('Unable to get todo', { error });
1313

1414
return {
15-
statusCode: HttpErrorCodes.IM_A_TEAPOT,
15+
statusCode: HttpStatusCodes.IM_A_TEAPOT,
1616
body: "I'm a teapot!",
1717
headers: {
1818
'x-correlation-id': reqCtx.request.headers.get('x-correlation-id'),

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
RestRouterOptions,
2222
RouteHandler,
2323
} from '../types/rest.js';
24-
import { HttpErrorCodes, HttpVerbs } from './constants.js';
24+
import { HttpStatusCodes, HttpVerbs } from './constants.js';
2525
import {
2626
handlerResultToProxyResult,
2727
handlerResultToWebResponse,
@@ -214,7 +214,7 @@ class Router {
214214
// We can't throw a MethodNotAllowedError outside the try block as it
215215
// will be converted to an internal server error by the API Gateway runtime
216216
return {
217-
statusCode: HttpErrorCodes.METHOD_NOT_ALLOWED,
217+
statusCode: HttpStatusCodes.METHOD_NOT_ALLOWED,
218218
body: '',
219219
};
220220
}
@@ -327,14 +327,15 @@ class Router {
327327
}
328328
if (!body.statusCode) {
329329
if (error instanceof NotFoundError) {
330-
body.statusCode = HttpErrorCodes.NOT_FOUND;
330+
body.statusCode = HttpStatusCodes.NOT_FOUND;
331331
} else if (error instanceof MethodNotAllowedError) {
332-
body.statusCode = HttpErrorCodes.METHOD_NOT_ALLOWED;
332+
body.statusCode = HttpStatusCodes.METHOD_NOT_ALLOWED;
333333
}
334334
}
335335
return new Response(JSON.stringify(body), {
336336
status:
337-
(body.statusCode as number) ?? HttpErrorCodes.INTERNAL_SERVER_ERROR,
337+
(body.statusCode as number) ??
338+
HttpStatusCodes.INTERNAL_SERVER_ERROR,
338339
headers: { 'Content-Type': 'application/json' },
339340
});
340341
} catch (handlerError) {

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const HttpVerbs = {
1+
const HttpVerbs = {
22
GET: 'GET',
33
POST: 'POST',
44
PUT: 'PUT',
@@ -8,7 +8,7 @@ export const HttpVerbs = {
88
OPTIONS: 'OPTIONS',
99
} as const;
1010

11-
export const HttpErrorCodes = {
11+
const HttpStatusCodes = {
1212
// 1xx Informational
1313
CONTINUE: 100,
1414
SWITCHING_PROTOCOLS: 101,
@@ -82,16 +82,16 @@ export const HttpErrorCodes = {
8282
NETWORK_AUTHENTICATION_REQUIRED: 511,
8383
} as const;
8484

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

87-
export const SAFE_CHARS = "-._~()'!*:@,;=+&$";
87+
const SAFE_CHARS = "-._~()'!*:@,;=+&$";
8888

89-
export const UNSAFE_CHARS = '%<> \\[\\]{}|^';
89+
const UNSAFE_CHARS = '%<> \\[\\]{}|^';
9090

9191
/**
9292
* Default CORS configuration
9393
*/
94-
export const DEFAULT_CORS_OPTIONS = {
94+
const DEFAULT_CORS_OPTIONS = {
9595
origin: '*',
9696
allowMethods: ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT'],
9797
allowHeaders: [
@@ -102,17 +102,28 @@ export const DEFAULT_CORS_OPTIONS = {
102102
'X-Amz-Security-Token',
103103
],
104104
exposeHeaders: [],
105-
credentials: false
105+
credentials: false,
106106
};
107107

108-
export const DEFAULT_COMPRESSION_RESPONSE_THRESHOLD = 1024;
108+
const DEFAULT_COMPRESSION_RESPONSE_THRESHOLD = 1024;
109109

110-
export const CACHE_CONTROL_NO_TRANSFORM_REGEX =
111-
/(?:^|,)\s*?no-transform\s*?(?:,|$)/i;
110+
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/i;
112111

113-
export const COMPRESSION_ENCODING_TYPES = {
112+
const COMPRESSION_ENCODING_TYPES = {
114113
GZIP: 'gzip',
115114
DEFLATE: 'deflate',
116115
IDENTITY: 'identity',
117116
ANY: '*',
118117
} as const;
118+
119+
export {
120+
HttpVerbs,
121+
HttpStatusCodes,
122+
PARAM_PATTERN,
123+
SAFE_CHARS,
124+
UNSAFE_CHARS,
125+
DEFAULT_CORS_OPTIONS,
126+
DEFAULT_COMPRESSION_RESPONSE_THRESHOLD,
127+
CACHE_CONTROL_NO_TRANSFORM_REGEX,
128+
COMPRESSION_ENCODING_TYPES,
129+
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
HandlerResponse,
55
HttpStatusCode,
66
} from '../types/rest.js';
7-
import { COMPRESSION_ENCODING_TYPES, HttpErrorCodes } from './constants.js';
7+
import { COMPRESSION_ENCODING_TYPES, HttpStatusCodes } from './constants.js';
88
import { isAPIGatewayProxyResult } from './utils.js';
99

1010
/**
@@ -190,7 +190,7 @@ export const handlerResultToWebResponse = (
190190
*/
191191
export const handlerResultToProxyResult = async (
192192
response: HandlerResponse,
193-
statusCode: HttpStatusCode = HttpErrorCodes.OK
193+
statusCode: HttpStatusCode = HttpStatusCodes.OK
194194
): Promise<APIGatewayProxyResult> => {
195195
if (isAPIGatewayProxyResult(response)) {
196196
return response;

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
22
import type { HandlerResponse, HttpStatusCode } from '../types/rest.js';
3-
import { HttpErrorCodes } from './constants.js';
3+
import { HttpStatusCodes } from './constants.js';
44

5-
export class RouteMatchingError extends Error {
5+
class RouteMatchingError extends Error {
66
constructor(
77
message: string,
88
public readonly path: string,
@@ -13,14 +13,14 @@ export class RouteMatchingError extends Error {
1313
}
1414
}
1515

16-
export class ParameterValidationError extends RouteMatchingError {
16+
class ParameterValidationError extends RouteMatchingError {
1717
constructor(public readonly issues: string[]) {
1818
super(`Parameter validation failed: ${issues.join(', ')}`, '', '');
1919
this.name = 'ParameterValidationError';
2020
}
2121
}
2222

23-
export abstract class ServiceError extends Error {
23+
abstract class ServiceError extends Error {
2424
abstract readonly statusCode: HttpStatusCode;
2525
abstract readonly errorType: string;
2626
public readonly details?: Record<string, unknown>;
@@ -47,8 +47,8 @@ export abstract class ServiceError extends Error {
4747
}
4848
}
4949

50-
export class BadRequestError extends ServiceError {
51-
readonly statusCode = HttpErrorCodes.BAD_REQUEST;
50+
class BadRequestError extends ServiceError {
51+
readonly statusCode = HttpStatusCodes.BAD_REQUEST;
5252
readonly errorType = 'BadRequestError';
5353

5454
constructor(
@@ -57,11 +57,12 @@ export class BadRequestError extends ServiceError {
5757
details?: Record<string, unknown>
5858
) {
5959
super(message, options, details);
60+
this.name = 'BadRequestError';
6061
}
6162
}
6263

63-
export class UnauthorizedError extends ServiceError {
64-
readonly statusCode = HttpErrorCodes.UNAUTHORIZED;
64+
class UnauthorizedError extends ServiceError {
65+
readonly statusCode = HttpStatusCodes.UNAUTHORIZED;
6566
readonly errorType = 'UnauthorizedError';
6667

6768
constructor(
@@ -70,11 +71,12 @@ export class UnauthorizedError extends ServiceError {
7071
details?: Record<string, unknown>
7172
) {
7273
super(message, options, details);
74+
this.name = 'UnauthorizedError';
7375
}
7476
}
7577

76-
export class ForbiddenError extends ServiceError {
77-
readonly statusCode = HttpErrorCodes.FORBIDDEN;
78+
class ForbiddenError extends ServiceError {
79+
readonly statusCode = HttpStatusCodes.FORBIDDEN;
7880
readonly errorType = 'ForbiddenError';
7981

8082
constructor(
@@ -83,11 +85,12 @@ export class ForbiddenError extends ServiceError {
8385
details?: Record<string, unknown>
8486
) {
8587
super(message, options, details);
88+
this.name = 'ForbiddenError';
8689
}
8790
}
8891

89-
export class NotFoundError extends ServiceError {
90-
readonly statusCode = HttpErrorCodes.NOT_FOUND;
92+
class NotFoundError extends ServiceError {
93+
readonly statusCode = HttpStatusCodes.NOT_FOUND;
9194
readonly errorType = 'NotFoundError';
9295

9396
constructor(
@@ -96,11 +99,12 @@ export class NotFoundError extends ServiceError {
9699
details?: Record<string, unknown>
97100
) {
98101
super(message, options, details);
102+
this.name = 'NotFoundError';
99103
}
100104
}
101105

102-
export class MethodNotAllowedError extends ServiceError {
103-
readonly statusCode = HttpErrorCodes.METHOD_NOT_ALLOWED;
106+
class MethodNotAllowedError extends ServiceError {
107+
readonly statusCode = HttpStatusCodes.METHOD_NOT_ALLOWED;
104108
readonly errorType = 'MethodNotAllowedError';
105109

106110
constructor(
@@ -109,11 +113,12 @@ export class MethodNotAllowedError extends ServiceError {
109113
details?: Record<string, unknown>
110114
) {
111115
super(message, options, details);
116+
this.name = 'MethodNotAllowedError';
112117
}
113118
}
114119

115-
export class RequestTimeoutError extends ServiceError {
116-
readonly statusCode = HttpErrorCodes.REQUEST_TIMEOUT;
120+
class RequestTimeoutError extends ServiceError {
121+
readonly statusCode = HttpStatusCodes.REQUEST_TIMEOUT;
117122
readonly errorType = 'RequestTimeoutError';
118123

119124
constructor(
@@ -122,11 +127,12 @@ export class RequestTimeoutError extends ServiceError {
122127
details?: Record<string, unknown>
123128
) {
124129
super(message, options, details);
130+
this.name = 'RequestTimeoutError';
125131
}
126132
}
127133

128-
export class RequestEntityTooLargeError extends ServiceError {
129-
readonly statusCode = HttpErrorCodes.REQUEST_ENTITY_TOO_LARGE;
134+
class RequestEntityTooLargeError extends ServiceError {
135+
readonly statusCode = HttpStatusCodes.REQUEST_ENTITY_TOO_LARGE;
130136
readonly errorType = 'RequestEntityTooLargeError';
131137

132138
constructor(
@@ -135,11 +141,12 @@ export class RequestEntityTooLargeError extends ServiceError {
135141
details?: Record<string, unknown>
136142
) {
137143
super(message, options, details);
144+
this.name = 'RequestEntityTooLargeError';
138145
}
139146
}
140147

141-
export class InternalServerError extends ServiceError {
142-
readonly statusCode = HttpErrorCodes.INTERNAL_SERVER_ERROR;
148+
class InternalServerError extends ServiceError {
149+
readonly statusCode = HttpStatusCodes.INTERNAL_SERVER_ERROR;
143150
readonly errorType = 'InternalServerError';
144151

145152
constructor(
@@ -148,11 +155,12 @@ export class InternalServerError extends ServiceError {
148155
details?: Record<string, unknown>
149156
) {
150157
super(message, options, details);
158+
this.name = 'InternalServerError';
151159
}
152160
}
153161

154-
export class ServiceUnavailableError extends ServiceError {
155-
readonly statusCode = HttpErrorCodes.SERVICE_UNAVAILABLE;
162+
class ServiceUnavailableError extends ServiceError {
163+
readonly statusCode = HttpStatusCodes.SERVICE_UNAVAILABLE;
156164
readonly errorType = 'ServiceUnavailableError';
157165

158166
constructor(
@@ -161,5 +169,21 @@ export class ServiceUnavailableError extends ServiceError {
161169
details?: Record<string, unknown>
162170
) {
163171
super(message, options, details);
172+
this.name = 'ServiceUnavailableError';
164173
}
165174
}
175+
176+
export {
177+
BadRequestError,
178+
ForbiddenError,
179+
InternalServerError,
180+
MethodNotAllowedError,
181+
NotFoundError,
182+
ParameterValidationError,
183+
RequestEntityTooLargeError,
184+
RequestTimeoutError,
185+
RouteMatchingError,
186+
ServiceError,
187+
ServiceUnavailableError,
188+
UnauthorizedError,
189+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { HttpErrorCodes, HttpVerbs } from './constants.js';
1+
export { HttpStatusCodes, HttpVerbs } from './constants.js';
22
export {
33
handlerResultToProxyResult,
44
handlerResultToWebResponse,

packages/event-handler/src/rest/middleware/cors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CorsOptions, Middleware } from '../../types/rest.js';
22
import {
33
DEFAULT_CORS_OPTIONS,
4-
HttpErrorCodes,
4+
HttpStatusCodes,
55
HttpVerbs,
66
} from '../constants.js';
77

@@ -123,7 +123,7 @@ export const cors = (options?: CorsOptions): Middleware => {
123123
reqCtx.res.headers.append('access-control-allow-headers', header);
124124
}
125125
return new Response(null, {
126-
status: HttpErrorCodes.NO_CONTENT,
126+
status: HttpStatusCodes.NO_CONTENT,
127127
headers: reqCtx.res.headers,
128128
});
129129
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
JSONObject,
44
} from '@aws-lambda-powertools/commons/types';
55
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
6-
import type { HttpErrorCodes, HttpVerbs } from '../rest/constants.js';
6+
import type { HttpStatusCodes, HttpVerbs } from '../rest/constants.js';
77
import type { Route } from '../rest/Route.js';
88
import type { Router } from '../rest/Router.js';
99
import type { ResolveOptions } from './common.js';
@@ -61,7 +61,7 @@ type RouteHandler<TReturn = HandlerResponse> = (
6161

6262
type HttpMethod = keyof typeof HttpVerbs;
6363

64-
type HttpStatusCode = (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes];
64+
type HttpStatusCode = (typeof HttpStatusCodes)[keyof typeof HttpStatusCodes];
6565

6666
type Path = `/${string}`;
6767

0 commit comments

Comments
 (0)