i wondering folks think way of handling errors in rest-style api written in clojure, using ring library.
one approach taken paul umbers in clojure restful api tutorial let exceptions happen naturally , allow them bubble way piece of middleware specialized in turning exceptions specific http status codes.
basically, db constraints throw own specific errors (e.g psqlexception), model validators throw type, under code 400 umbrella. unknown exceptions caught generic exception handler , return 500 code.
a few thoughts:
- can better? wrong design specific reason?
- many claim handling generic exception type bad practice. can such argument made here well?
thanks!
i'm no means expert in particular area, since no 1 else has weighed in, i'll give 2 cents.
the solution linked seems reasonable approach me. given small amount of cooperation between handlers , exception middleware, tag exceptions additional information might useful when rendering error response, without actual details of error handling creeping application logic.
so first question: it's possible tailor system more given specific use case, general-purpose error handling scheme, seems pretty good. there's nothing jumps out @ me straight-up "wrong".
to second question: it's bad practice catch generic exception type when know you're looking more specific one, because want avoid conflating expected , unexpected errors. if know there's possibility of missingresourceexception when bundle lookup, wouldn't want exception handler accidentally bury nullpointerexception bubbling actual bug in code.
in case, though, argue catching generic exception type right thing doing. rather handling specific conditions missingresourceexception, goal of top-level handler catch application logic doesn't , convert error information that's meaningful client of api. it's sort of last line of defense between implementation , consumer @ other end.
Comments
Post a Comment