-
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.
- Loading branch information
1 parent
8302335
commit 0d14148
Showing
8 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const enum ErrorType { | ||
NOT_FOUND = "NOT_FOUND", | ||
UNEXPECTED_ERROR = "UNEXPECTED_ERROR" | ||
} | ||
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,14 @@ | ||
import {ErrorType} from "./errorType"; | ||
|
||
export class GroutError extends Error { | ||
status: number; | ||
errorType: ErrorType; | ||
|
||
constructor(message: string, status: number, errorType: ErrorType) { | ||
super(message); | ||
|
||
this.name = "GroutError"; | ||
this.status = status; | ||
this.errorType = errorType; | ||
} | ||
} | ||
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,24 @@ | ||
import { Request, Response } from "express"; | ||
import { uid } from "uid"; | ||
import {GroutError} from "./groutError"; | ||
import {ErrorType} from "./errorType"; | ||
import {reqWithError} from "../logging"; | ||
import {jsonResponseError} from "../jsonResponse"; | ||
|
||
export const handleError = (err: Error, req: Request, res: Response, _: Function) => { | ||
Check failure on line 8 in src/errors/handleError.ts GitHub Actions / lint
|
||
const groutError = err instanceof GroutError; | ||
|
||
const status = groutError ? err.status : 500; | ||
const type = groutError ? err.errorType : ErrorType.UNEXPECTED_ERROR; | ||
|
||
// Do not return raw messages from unexpected errors to the front end | ||
const detail = groutError ? err.message | ||
: `An unexpected error occurred. Please contact support and quote error code ${uid()}`; | ||
|
||
// Set error type, detail and stack on req so morgan logs them | ||
reqWithError(req).errorType = type; | ||
reqWithError(req).errorDetail = detail; | ||
reqWithError(req).errorStack = err.stack; | ||
|
||
jsonResponseError(status, type, detail, res); | ||
} |
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,24 @@ | ||
import { Response } from "express"; | ||
|
||
//TODO: use json response on success for index response (and metadata). Can't use it for tile data! | ||
const addContentType = (res: Response) => { | ||
res.header("Content-Type", "application/json"); | ||
}; | ||
|
||
export const jsonResponseError = ( | ||
httpStatus: number, | ||
error: string, | ||
detail: string, | ||
res: Response | ||
) => { | ||
addContentType(res); | ||
const responseObject = { | ||
status: "failure", | ||
errors: [ | ||
{ error, detail } | ||
], | ||
data: null | ||
}; | ||
res.status(httpStatus); | ||
res.end(JSON.stringify(responseObject)); | ||
}; |
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