added statuscode in safehandler
This commit is contained in:
@@ -2,22 +2,47 @@
|
||||
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>
|
||||
handler: (event: APIGatewayProxyEvent, context?: Context) => Promise<APIGatewayProxyResult | any>
|
||||
): ((event: APIGatewayProxyEvent, context: Context) => Promise<APIGatewayProxyResult>) => {
|
||||
return async (event, context) => {
|
||||
try {
|
||||
const result = await handler(event, context);
|
||||
return (
|
||||
result ?? {
|
||||
|
||||
// If handler returned null/undefined → return 204 response
|
||||
if (!result) {
|
||||
return {
|
||||
statusCode: 204,
|
||||
body: '',
|
||||
}
|
||||
);
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
statusCode: 204,
|
||||
message: 'No content',
|
||||
data: null,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
// If handler returned a structured Lambda response
|
||||
if (result.statusCode && result.body) {
|
||||
return {
|
||||
statusCode: result.statusCode,
|
||||
headers: result.headers || {},
|
||||
body: injectStatusCodeIntoBody(result.body, result.statusCode),
|
||||
};
|
||||
}
|
||||
|
||||
// If handler returned plain data (not wrapped)
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'OK',
|
||||
statusCode: 200,
|
||||
data: result,
|
||||
}),
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error('Error occurred:', error);
|
||||
console.error('❌ Error occurred:', error);
|
||||
|
||||
if (error instanceof ApiError) {
|
||||
return {
|
||||
@@ -25,31 +50,52 @@ export const safeHandler = (
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
message: error.message,
|
||||
statusCode: error.statusCode,
|
||||
data: null,
|
||||
error: {
|
||||
code: error.statusCode,
|
||||
description: error.message,
|
||||
statusCode: error.statusCode,
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }),
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack }),
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
// Internal Server Error fallback
|
||||
return {
|
||||
statusCode: 500,
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
message: 'Internal server error',
|
||||
statusCode: 500,
|
||||
data: null,
|
||||
error: {
|
||||
code: 500,
|
||||
description: 'Internal server error',
|
||||
statusCode: 500,
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }),
|
||||
...(process.env.STAGE !== 'prod' && { debug: error.stack }),
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
// Utility: safely inject statusCode into the JSON response body
|
||||
function injectStatusCodeIntoBody(body: string, statusCode: number): string {
|
||||
try {
|
||||
const json = JSON.parse(body);
|
||||
json.statusCode = statusCode;
|
||||
return JSON.stringify(json);
|
||||
} catch {
|
||||
// If body is not JSON, wrap it
|
||||
return JSON.stringify({
|
||||
success: true,
|
||||
statusCode,
|
||||
message: body,
|
||||
data: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
import config from '@/config/config';
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { HostService } from '../../host/services/host.service';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost';
|
||||
import {
|
||||
hostCompanyDetailsSchema,
|
||||
REQUIRED_DOC_TYPES,
|
||||
hostDocumentsSchema,
|
||||
parentCompanySchema,
|
||||
} from '../../../common/utils/validation/host/hostCompanyDetails.validation';
|
||||
import AWS from 'aws-sdk';
|
||||
import Busboy from 'busboy';
|
||||
import crypto from 'crypto';
|
||||
import config from '@/config/config';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import {
|
||||
hostCompanyDetailsSchema,
|
||||
parentCompanySchema,
|
||||
REQUIRED_DOC_TYPES
|
||||
} from '../../../common/utils/validation/host/hostCompanyDetails.validation';
|
||||
import { HostService } from '../../host/services/host.service';
|
||||
|
||||
const prisma = new PrismaService();
|
||||
const hostService = new HostService(prisma);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { HostService } from '../services/host.service';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { generateOtpHelper } from '../../../common/utils/helper/sendOtp';
|
||||
import { HostService } from '../services/host.service';
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
const hostService = new HostService(prismaService);
|
||||
@@ -34,7 +33,7 @@ export const handler = safeHandler(async (
|
||||
});
|
||||
|
||||
if (user && user.userPassword) {
|
||||
throw new ApiError(404, 'User is already registered. Please login.');
|
||||
throw new ApiError(409, 'User is already registered. Please login.');
|
||||
}
|
||||
|
||||
let newUser;
|
||||
|
||||
Reference in New Issue
Block a user