-
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.
feat: create uniform error structure (#115)
Signed-off-by: james-a-morris <[email protected]>
- Loading branch information
1 parent
f2b8e95
commit df6c94d
Showing
19 changed files
with
118 additions
and
62 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
isIndexerError, | ||
isIndexerHTTPError, | ||
StatusCodes, | ||
} from "@repo/error-handling"; | ||
import { Request, Response, NextFunction } from "express"; | ||
import { isHttpError } from "./express-app"; | ||
import { StructError } from "superstruct"; | ||
|
||
const DEFAULT_STATUS = StatusCodes.BAD_REQUEST; | ||
|
||
const errorHandler = ( | ||
err: unknown, | ||
req: Request, | ||
res: Response, | ||
_: NextFunction, | ||
): void => { | ||
// At a base level we need to confirm that this isn't a valid | ||
// passthrough - if so ignore | ||
if (isIndexerError(err)) { | ||
// If we have a custom sub-type to specify the error code, use it | ||
// otherwise default to a status 400 | ||
const httpStatus = isIndexerHTTPError(err) | ||
? err.httpStatusCode | ||
: DEFAULT_STATUS; | ||
res.status(httpStatus).json(err.toJSON()); | ||
} else if (isHttpError(err)) { | ||
res.status(err.status ?? DEFAULT_STATUS).json({ | ||
message: err.message, | ||
error: "NavigationError", | ||
}); | ||
} else if (err instanceof StructError) { | ||
res.status(StatusCodes.BAD_REQUEST).json({ | ||
error: "ValidationError", | ||
message: err.message, | ||
}); | ||
} else if (err instanceof Error) { | ||
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ | ||
error: "UnknownError", | ||
message: err.message, | ||
}); | ||
} | ||
}; | ||
|
||
export default errorHandler; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
import { HttpError } from "../express-app"; | ||
import { HttpStatus } from "../model/httpStatus"; | ||
import { IndexerHTTPError, StatusCodes } from "@repo/error-handling"; | ||
|
||
export class DepositNotFoundException extends HttpError { | ||
export class DepositNotFoundException extends IndexerHTTPError { | ||
constructor() { | ||
super("Deposit not found"); | ||
this.name = "DepositNotFoundException"; | ||
this.status = HttpStatus.NOT_FOUND; | ||
super( | ||
StatusCodes.NOT_FOUND, | ||
DepositNotFoundException.name, | ||
"Deposit not found given the provided constraints", | ||
); | ||
} | ||
} | ||
|
||
export class IndexParamOutOfRangeException extends HttpError { | ||
export class IndexParamOutOfRangeException extends IndexerHTTPError { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "IndexParamOutOfRangeException"; | ||
this.status = HttpStatus.BAD_REQUEST; | ||
super(StatusCodes.BAD_REQUEST, IndexParamOutOfRangeException.name, message); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import assert from "assert"; | ||
import { assert } from "@repo/error-handling"; | ||
import Redis from "ioredis"; | ||
|
||
/** | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.