Menyelesaikan object user dan init base
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
core/exceptions/
|
||||
|
||||
Penjelasan:
|
||||
Berisi global exception filters dan custom exception classes.
|
||||
|
||||
Isi yang direkomendasikan:
|
||||
- http-exception.filter.ts
|
||||
- domain-exception.ts (custom domain exception base)
|
||||
|
||||
Aturan:
|
||||
- Exception filter mengubah exception menjadi response standar API.
|
||||
- Gunakan kode error konsisten (error code constant) agar client dapat menangani error.
|
||||
@@ -0,0 +1,18 @@
|
||||
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
|
||||
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse();
|
||||
const status = exception.getStatus ? exception.getStatus() : 500;
|
||||
|
||||
response.status(status).json({
|
||||
success: false,
|
||||
error: {
|
||||
code: exception.code || 'INTERNAL_ERROR',
|
||||
message: exception.message || 'Internal server error',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user