forked from humanprotocol/human-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling in reputation oracle (humanprotocol#2018)
- Loading branch information
Showing
49 changed files
with
470 additions
and
382 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
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
packages/apps/reputation-oracle/server/src/common/errors/controlled.ts
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
export class ControlledError extends Error { | ||
status: HttpStatus; | ||
|
||
constructor(message: string, status: HttpStatus, stack?: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.status = status; | ||
if (stack) this.stack = stack; | ||
else Error.captureStackTrace(this, this.constructor); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...cle/server/src/database/database.error.ts → ...acle/server/src/common/errors/database.ts
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
35 changes: 0 additions & 35 deletions
35
packages/apps/reputation-oracle/server/src/common/exceptions/auth.filter.ts
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
packages/apps/reputation-oracle/server/src/common/exceptions/database.filter.ts
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
packages/apps/reputation-oracle/server/src/common/exceptions/exception.filter.ts
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter as IExceptionFilter, | ||
HttpStatus, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { DatabaseError } from '../errors/database'; | ||
import { ControlledError } from '../errors/controlled'; | ||
|
||
@Catch() | ||
export class ExceptionFilter implements IExceptionFilter { | ||
private logger = new Logger(ExceptionFilter.name); | ||
|
||
catch(exception: any, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const request = ctx.getRequest<Request>(); | ||
|
||
let status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
let message = 'Internal server error'; | ||
|
||
if (exception instanceof ControlledError) { | ||
status = exception.status; | ||
message = exception.message; | ||
|
||
this.logger.error(`Reputation Oracle error: ${message}`, exception.stack); | ||
} else if (exception instanceof DatabaseError) { | ||
status = HttpStatus.UNPROCESSABLE_ENTITY; | ||
message = `Database error: ${exception.message}`; | ||
|
||
this.logger.error(message, exception.stack); | ||
} else { | ||
this.logger.error( | ||
`Unhandled exception: ${exception.message}`, | ||
exception.stack, | ||
); | ||
} | ||
|
||
response.status(status).json({ | ||
statusCode: status, | ||
timestamp: new Date().toISOString(), | ||
message: message, | ||
path: request.url, | ||
}); | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
packages/apps/reputation-oracle/server/src/common/exceptions/kyc.filter.ts
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
packages/apps/reputation-oracle/server/src/common/exceptions/reputation.filter.ts
This file was deleted.
Oops, something went wrong.
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
1 change: 0 additions & 1 deletion
1
packages/apps/reputation-oracle/server/src/common/interceptors/index.ts
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
packages/apps/reputation-oracle/server/src/common/interceptors/not-found.ts
This file was deleted.
Oops, something went wrong.
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.