-
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.
fix(createkeyfunc): corrigir problemas de escape de erros relacionado…
… a entidades existentes
- Loading branch information
1 parent
62d6030
commit 706a60e
Showing
1 changed file
with
23 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,47 @@ | ||
import { Response } from "@google-cloud/functions-framework"; | ||
import { FirestoreCustomError, FirestoreCustomErrorTag } from "../errors/firestore"; | ||
import { ResponseBodyModel } from "../utils/response"; | ||
import { ErrorFilter } from "./contract" | ||
import { LoggerAdapter } from "../config/logger/contract"; | ||
import { Response } from '@google-cloud/functions-framework'; | ||
import { | ||
FirestoreCustomError, | ||
FirestoreCustomErrorTag, | ||
} from '../errors/firestore'; | ||
import { ResponseBodyModel } from '../utils/response'; | ||
import { ErrorFilter } from './contract'; | ||
import { LoggerAdapter } from '../config/logger/contract'; | ||
|
||
export class FirestoreCustomErrorFilter implements ErrorFilter { | ||
constructor(private readonly res: Response, private readonly logger: LoggerAdapter) {} | ||
constructor( | ||
private readonly res: Response, | ||
private readonly logger: LoggerAdapter, | ||
) {} | ||
|
||
private readonly filterMsg = [ | ||
{ | ||
match: 'entity already exists', | ||
match: 'ALREADY_EXISTS', | ||
httpCode: 401, | ||
message: 'You don\'t have permission to access this route', | ||
name: 'Unauthorized' | ||
name: 'Unauthorized', | ||
}, | ||
{ | ||
match: FirestoreCustomErrorTag.malformedEntity, | ||
httpCode: 500, | ||
message: 'Somethig wrong happened', | ||
name: 'Internal Server Error' | ||
} | ||
] | ||
name: 'Internal Server Error', | ||
}, | ||
]; | ||
|
||
exec(err: FirestoreCustomError, sessionId: string) { | ||
const possibleResponse = this.filterMsg.find( | ||
(item) => err.cause.includes(item.match) || err.tag === item.match | ||
) | ||
(item) => err.cause.includes(item.match) || err.tag === item.match, | ||
); | ||
|
||
this.logger.error({ | ||
sessionId, | ||
error: err | ||
}) | ||
error: err, | ||
}); | ||
|
||
ResponseBodyModel.start(this.res).send({ | ||
statusCode: possibleResponse?.httpCode ?? 500, | ||
message: possibleResponse?.message ?? 'Somethig wrong happened', | ||
name: possibleResponse?.name ?? 'Internal Server Error' | ||
}) | ||
name: possibleResponse?.name ?? 'Internal Server Error', | ||
}); | ||
} | ||
} |