-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add 'PGRST' error code to differentiate from PostgreSQL errors #1926
Conversation
Right now, the
This combination is... not really useful. Or let's say it does not really achieve either of the following two goals:
One way to achieve both would be to add another field, e.g. called Maybe just rename the |
True, the PGRST is not helpful because it contains "PG", which could be interpreted as a PostgreSQL error.
I like this one better. I'd say just have a |
Hm, though having another field could break some libraries with strict typing for the error body. That was mostly the main idea for just having two additional codes: |
One problem with prefixing the code, with say |
@wolfgangwalther @steve-chavez I like this approach too. My initial suggestion was to add
I think we should arrive to an agreement on how to name these error codes, if at all. Otherwise, just the new |
One thing with adding the The docs for this PR made a lot of sense and it was great that we could map our error fields to pg error fields(MESSAGE, DETAIL, HINT, ERRCODE). With the So I think we should just go back to |
Well, let's just make it a code prefix then and give all our errors a |
src/PostgREST/Error.hs
Outdated
data ErrorCodePrefix | ||
= ApiRequestErrorPrefix Text | ||
| HasqlErrorPrefix Text | ||
| GeneralErrorPrefix Text | ||
|
||
instance JSON.ToJSON ErrorCodePrefix where | ||
toJSON (GeneralErrorPrefix c) = JSON.toJSON ("PGRST0" <> c) | ||
toJSON (ApiRequestErrorPrefix c) = JSON.toJSON ("PGRST1" <> c) | ||
toJSON (HasqlErrorPrefix c) = JSON.toJSON ("PGRST2" <> c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it convenient to add a prefix for each group of errors that are defined in the code. This is to keep an order in the file and to identify the groups more easily. If no prefix was present, then the sequence of error codes would "jump" from group to group; e. g. if the last Error
has a PGRST30
code, and the last ApiRequestError
has a PGRST10
then the next ApiRequestError
added would be PGRST31
.
The GerenalErrorPrefix
corresponds to the Error
data type and has a prefix of 0
(because it has the errors that are not "grouped" by default) and has ApiRequestError
and PgError
(Hasql errors) as constructors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that might be useful and it's similar to how pg classifies their errors: https://www.postgresql.org/docs/13/errcodes-appendix.html#ERRCODES-TABLE
So one group could be for JWT related errors, another for API errors, another for connection errors and so on.
@wolfgangwalther This is a great idea that Laurence is currently working on. Also, regarding a URL on the error body, I've just seen the Problem Details for HTTP APIs RFC(7807) that presents a |
e44eb7f
to
24a1122
Compare
The RFC mentions that:
So the above idea about specifying |
I don't think we need any base URI to configure here. The URIs will all point to PostgREST docs, no? That will be an absolute URI in all cases. Note, that the RFC does not say that URIs "must" be relative. It merely says, relative URIs are accepted, too, and if used, they must resolve in the matter described. There are some examples in the RFC that use absolute URIs and it also has this:
|
Hm, I don't think it would be right to do that. I assume that external services(example) or open data initiatives(example) that use us for their API would like to keep their users on their own site, so they should be able to override the error base URI. Also, I'm not sure if we should add the |
a3a56c8
to
f058ff7
Compare
HasqlErrorCode00 -> "400" | ||
HasqlErrorCode01 -> "401" | ||
HasqlErrorCode02 -> "402" | ||
HasqlErrorCode03 -> "403" | ||
HasqlErrorCode04 -> "404" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codecov is giving a warning for these errors codes and their error instances, but I don't think they can be tested because they are caused by unexpected bugs. We might have to tank the score loss here.
Maybe we can add a base URI for that later, but I don't think we need it right away. A base URI would require to have those external services have the same general URI structure, where just the base can be replaced. A more flexible approach would be to rewrite those error responses via nginx
If we indeed move the OpenAPI output to "contrib", we shouldn't assume we have OpenAPI at all in the main app. I think linking to our docs via the type field (or any other field, |
Co-authored-by: Steve Chavez <[email protected]>
Closes #1917
Todo: