made first lambda function and deployed it successfully
This commit is contained in:
55
src/common/utils/handlers/safeHandler.ts
Normal file
55
src/common/utils/handlers/safeHandler.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
// safeHandler.ts
|
||||
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from 'aws-lambda';
|
||||
import ApiError from '../helper/ApiError';
|
||||
|
||||
const stage = process.env.STAGE ?? 'dev';
|
||||
|
||||
export const safeHandler = (
|
||||
handler: (event: APIGatewayProxyEvent, context?: Context) => Promise<APIGatewayProxyResult | undefined>
|
||||
): ((event: APIGatewayProxyEvent, context: Context) => Promise<APIGatewayProxyResult>) => {
|
||||
return async (event, context) => {
|
||||
try {
|
||||
const result = await handler(event, context);
|
||||
return (
|
||||
result ?? {
|
||||
statusCode: 204,
|
||||
body: '',
|
||||
}
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.error('Error occurred:', error);
|
||||
|
||||
if (error instanceof ApiError) {
|
||||
return {
|
||||
statusCode: error.statusCode,
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
message: error.message,
|
||||
data: null,
|
||||
error: {
|
||||
code: error.statusCode,
|
||||
description: error.message,
|
||||
statusCode: error.statusCode,
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }),
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 500,
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
message: 'Internal server error',
|
||||
data: null,
|
||||
error: {
|
||||
code: 500,
|
||||
description: 'Internal server error',
|
||||
statusCode: 500,
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }),
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user