starlette-context icon indicating copy to clipboard operation
starlette-context copied to clipboard

drop error_response, add error detail and support for serialization

Open tomwojcik opened this issue 3 years ago • 6 comments

Closes https://github.com/tomwojcik/starlette-context/issues/58

tomwojcik avatar Dec 28 '21 15:12 tomwojcik

I'll have a look at it over the new year

hhamana avatar Dec 29 '21 04:12 hhamana

I believe changes from this implementation will do the trick.

It doesn't. My API needs to have a consistent error schema with the rest of the system, which is a quite custom JSON response. This kills any way for user to customize it to their needs, and enforces a hard-coded non-JSON error message without alternative. I get that you don't like passing a response object through the exception, and I'd want to agree there, but the way plugins work, that was the most straightforward way to escalate it to the handler As this pr hints at, the exception itself can define the response. Sure, but then we need to allow customizing the exception raised, and also modify headers (I'll want the "Content-Type" header to be application/json for a proper JSON response at least) and we'll have to build the appropriate response from scratch in the middleware given the exception's data. I think it's more convenient to have it already build for us there instead, but you'll notice this'll get us back to pretty much the current situation.

Having the default error message as f"Invalid UUID in request header {self.key}" is better for sure though.

hhamana avatar Dec 30 '21 05:12 hhamana

My API needs to have a consistent error schema with the rest of the system, which is a quite custom JSON response.

AFAIK that's impossible. Unless you have another layer on top of your starlette-based app, starlette errors return 500 plain text. possibly related https://github.com/encode/starlette/issues/1175#issuecomment-882898895

But I get you. I don't see why both solutions shouldn't coexist, so a default error message and optionally a custom response for errors. I will think about it.

tomwojcik avatar Jan 01 '22 20:01 tomwojcik

My API needs to have a consistent error schema with the rest of the system, which is a quite custom JSON response. This kills any way for user to customize it to their needs, and enforces a hard-coded non-JSON error message without alternative.

https://github.com/tomwojcik/starlette-context/pull/59/files#diff-4a0aecbfe7f993c2281b848a8b4269204420d30388706ddea3bc26dbfd2b1171R51-R64

wdyt?

tomwojcik avatar Jan 01 '22 22:01 tomwojcik

Unless you have another layer on top of your starlette-based app, starlette errors return 500 plain text.

that's if you leave those as errors though. If you handle those and make a response from there somehow, the Starlette layer won't even see it's been an exception.

hhamana avatar Jan 02 '22 01:01 hhamana

wdyt?

I guess the factory approach is workable. It reduces the plugin-specific response to a singular handler on the middleware, but one use I can see is raising the exception internally, to except and extract the exact exception (as different plugins will still raise different subclasses), and a custom plugin can still have its own exception mixed in too.

hhamana avatar Jan 02 '22 01:01 hhamana