Skip to content

Commit 835cba2

Browse files
committed
fixed errors
1 parent 477594d commit 835cba2

File tree

3 files changed

+122
-141
lines changed

3 files changed

+122
-141
lines changed

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
2-
import { isDevMode } from '@aws-lambda-powertools/commons/utils/env';
2+
import type { StandardSchemaV1 } from '@standard-schema/spec';
33
import type { HandlerResponse, HttpStatusCode } from '../types/http.js';
44
import { HttpStatusCodes } from './constants.js';
55

@@ -174,36 +174,21 @@ class ServiceUnavailableError extends HttpError {
174174
}
175175
}
176176

177-
class InvalidEventError extends Error {
178-
constructor(message?: string) {
179-
super(message);
180-
this.name = 'InvalidEventError';
181-
}
182-
}
183-
184-
class InvalidHttpMethodError extends Error {
185-
constructor(method: string) {
186-
super(`HTTP method ${method} is not supported.`);
187-
this.name = 'InvalidEventError';
188-
}
189-
}
190-
191177
class RequestValidationError extends HttpError {
192178
readonly statusCode = HttpStatusCodes.UNPROCESSABLE_ENTITY;
193179
readonly errorType = 'RequestValidationError';
194180

195181
constructor(
196-
message: string,
197-
public readonly component: 'body' | 'headers' | 'path' | 'query',
198-
public readonly originalError?: Error,
199-
details?: Record<string, unknown>
182+
message?: string,
183+
issues?: StandardSchemaV1.FailureResult['issues'],
184+
options?: ErrorOptions
200185
) {
201-
const errorDetails =
202-
isDevMode() && originalError
203-
? { ...details, validationError: originalError.message }
204-
: details;
205-
206-
super(message, { cause: originalError }, errorDetails);
186+
super(message, options, {
187+
issues: issues?.map((issue) => ({
188+
message: issue.message,
189+
path: issue.path,
190+
})),
191+
});
207192
this.name = 'RequestValidationError';
208193
}
209194
}
@@ -213,21 +198,34 @@ class ResponseValidationError extends HttpError {
213198
readonly errorType = 'ResponseValidationError';
214199

215200
constructor(
216-
message: string,
217-
public readonly component: 'body' | 'headers',
218-
public readonly originalError?: Error,
219-
details?: Record<string, unknown>
201+
message?: string,
202+
issues?: StandardSchemaV1.FailureResult['issues'],
203+
options?: ErrorOptions
220204
) {
221-
const errorDetails =
222-
isDevMode() && originalError
223-
? { ...details, validationError: originalError.message }
224-
: details;
225-
226-
super(message, { cause: originalError }, errorDetails);
205+
super(message, options, {
206+
issues: issues?.map((issue) => ({
207+
message: issue.message,
208+
path: issue.path,
209+
})),
210+
});
227211
this.name = 'ResponseValidationError';
228212
}
229213
}
230214

215+
class InvalidEventError extends Error {
216+
constructor(message?: string) {
217+
super(message);
218+
this.name = 'InvalidEventError';
219+
}
220+
}
221+
222+
class InvalidHttpMethodError extends Error {
223+
constructor(method: string) {
224+
super(`HTTP method ${method} is not supported.`);
225+
this.name = 'InvalidEventError';
226+
}
227+
}
228+
231229
export {
232230
BadRequestError,
233231
ForbiddenError,

packages/event-handler/src/http/middleware/validation.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,18 @@ async function extractBody(source: Request | Response): Promise<unknown> {
121121
if (source instanceof Request) {
122122
throw new RequestValidationError(
123123
'Validation failed for request body',
124-
'body',
125-
new Error('Invalid JSON')
124+
[],
125+
{
126+
cause: new Error('Invalid JSON body'),
127+
}
126128
);
127129
}
128130
throw new ResponseValidationError(
129131
'Validation failed for response body',
130-
'body',
131-
new Error('Invalid JSON')
132+
[],
133+
{
134+
cause: new Error('Invalid JSON body'),
135+
}
132136
);
133137
}
134138
}
@@ -155,8 +159,7 @@ async function validateRequest<T>(
155159

156160
if ('issues' in result) {
157161
const message = `Validation failed for request ${component}`;
158-
const error = new Error('Validation failed');
159-
throw new RequestValidationError(message, component, error);
162+
throw new RequestValidationError(message, result.issues);
160163
}
161164

162165
return result.value as T | Record<string, string>;
@@ -181,8 +184,7 @@ async function validateResponse<T>(
181184

182185
if ('issues' in result) {
183186
const message = `Validation failed for response ${component}`;
184-
const error = new Error('Validation failed');
185-
throw new ResponseValidationError(message, component, error);
187+
throw new ResponseValidationError(message, result.issues);
186188
}
187189

188190
return result.value as T | Record<string, string>;

0 commit comments

Comments
 (0)