made first lambda function and deployed it successfully

This commit is contained in:
2025-11-12 11:35:06 +05:30
parent 0230105958
commit 5399c8b987
6 changed files with 1752 additions and 225 deletions

View 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 }),
},
}),
};
}
};
};