-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from credebl/354-refactor-ledger-service
refactor: Create Schema
- Loading branch information
Showing
13 changed files
with
155 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,49 @@ | ||
import { Catch, ArgumentsHost, HttpException, HttpStatus, Logger } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { isArray } from 'class-validator'; | ||
import { ExceptionResponse } from './interface'; | ||
import { ResponseMessages } from '@credebl/common/response-messages'; | ||
|
||
@Catch() | ||
export class CustomExceptionFilter extends BaseExceptionFilter { | ||
private readonly logger = new Logger(); | ||
catch(exception: HttpException, host: ArgumentsHost): void { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse(); | ||
|
||
|
||
let errorResponse; | ||
let status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
if (exception instanceof HttpException) { | ||
status = exception.getStatus(); | ||
} | ||
|
||
|
||
this.logger.error(`exception ::: ${JSON.stringify(exception)}`); | ||
|
||
if ("Cannot read properties of undefined (reading 'response')" === exception.message) { | ||
exception.message = 'Oops! Something went wrong. Please try again'; | ||
} | ||
|
||
let errorResponse; | ||
if (isArray(exception)) { | ||
if (!exception || '{}' === JSON.stringify(exception)) { | ||
errorResponse = { | ||
statusCode: status, | ||
message: exception[0], | ||
error: exception[0] | ||
}; | ||
} else if (exception && exception['statusCode'] === HttpStatus.INTERNAL_SERVER_ERROR) { | ||
if (exception.message && exception.message['message']) { | ||
errorResponse = { | ||
statusCode: status, | ||
message: exception.message['message'], | ||
error: exception.message['message'] | ||
}; | ||
} else { | ||
errorResponse = { | ||
statusCode: status, | ||
message: 'Oops! Something went wrong. Please try again', | ||
error: 'Oops! Something went wrong. Please try again' | ||
}; | ||
} | ||
} else if ( | ||
exception && | ||
exception['error'] && | ||
exception['error'].message && | ||
(exception['error'].statusCode || exception['error'].code) | ||
) { | ||
const statusCode = exception['error'].statusCode || exception['error'].code || status; | ||
errorResponse = { | ||
statusCode, | ||
message: exception['error'].message || 'Internal server error', | ||
error: exception['error'].message || 'Internal server error' | ||
}; | ||
} else if (exception && exception['statusCode'] === undefined && status === HttpStatus.INTERNAL_SERVER_ERROR) { | ||
errorResponse = { | ||
statusCode: status, | ||
message: 'Oops! Something went wrong. Please try again', | ||
error: 'Oops! Something went wrong. Please try again' | ||
message: 'Something went wrong!', | ||
error: ResponseMessages.errorMessages.serverError | ||
}; | ||
} | ||
if (exception instanceof HttpException) { | ||
status = exception.getStatus(); | ||
} | ||
|
||
let exceptionResponse: ExceptionResponse; | ||
|
||
if (exception['response']) { | ||
exceptionResponse = exception['response']; | ||
} else { | ||
if (exception && exception['response'] && exception.message) { | ||
if (Array.isArray(exception['response'].message)) { | ||
errorResponse = { | ||
statusCode: exception['statusCode'] ? exception['statusCode'] : status, | ||
message: exception.message ? exception.message : 'Internal server error', | ||
error: exception['response'].message | ||
? exception['response'].message | ||
: exception['response'] | ||
? exception['response'] | ||
: 'Internal server error' | ||
}; | ||
} else { | ||
errorResponse = { | ||
statusCode: exception['statusCode'] ? exception['statusCode'] : status, | ||
message: exception['response'].message | ||
? exception['response'].message | ||
: exception['response'] | ||
? exception['response'] | ||
: 'Internal server error', | ||
error: exception['response'].message | ||
? exception['response'].message | ||
: exception['response'] | ||
? exception['response'] | ||
: 'Internal server error' | ||
}; | ||
} | ||
} else if (exception && exception.message) { | ||
errorResponse = { | ||
statusCode: exception['statusCode'] ? exception['statusCode'] : status, | ||
message: exception.message || 'Internal server error', | ||
error: exception.message || 'Internal server error' | ||
}; | ||
} | ||
exceptionResponse = exception as unknown as ExceptionResponse; | ||
} | ||
|
||
errorResponse = { | ||
statusCode: exceptionResponse.statusCode ? exceptionResponse.statusCode : status, | ||
message: exceptionResponse.message | ||
? exceptionResponse.message | ||
: 'Something went wrong!', | ||
error: exceptionResponse.error | ||
? exceptionResponse.error | ||
: ResponseMessages.errorMessages.serverError | ||
}; | ||
|
||
response.status(errorResponse.statusCode).json(errorResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.