Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mrc-6085 Error handling #3

Draft
wants to merge 7 commits into
base: mrc-6064-serve-tile-data
Choose a base branch
from

Conversation

EmmaLRussell
Copy link
Contributor

@EmmaLRussell EmmaLRussell commented Dec 9, 2024

This branch implements an error handling framework. This is based on the framework implemented in WODIN which seems to work quite well! The idea is that any requests which cannot be completed successfully should cause an Error to be thrown. This Error will be dealt with by the handleError method which will ensure that the appropriate JSON error response and status code are returned.

Key changes:

  • GroutError class. This is for errors which we throw deliberately e.g. in the case of a bad request, or unknown resource requests. Parameters to this class include message, ErrorType and status code. These parameters are used by handleError to build the error Response.
  • handleError - given a thrown error, builds a "Porcelain-style" response with error, status code and error type. If the error is an instance of GroutError its message is returned in the response. If not, then this is an unexpected error, and we don't want to return its raw error message to the client. so we include a generic message, along with a unique error code. handleError includes message (including error code), type and stack in tokens to be logged by morgan. This means that we can find the stack corresponding to an unexpected error code in the logs if required.
  • asyncControllerHandler - this allows handleError to be triggered when the error is thrown asynchronously to the initial request call. It does this by catching any error, and ensuring that next is called with the error as parameter. This handler should be used by any controller method which is async.
  • notFound - utility function for throwing a GroutError for any 404 scenario. Used by both the router and the TileController (for the case where the tile request was for unknown data).
  • jsonResponse utility for returning JSON error and success responses in our standard Porcelain format. Update to the index response to use this.

@EmmaLRussell EmmaLRussell changed the base branch from main to mrc-6064-serve-tile-data December 9, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant