-
Notifications
You must be signed in to change notification settings - Fork 0
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
Apply spec compliant error formatting #10
Conversation
a359d90
to
fd8b815
Compare
"fields": [ | ||
{ | ||
"message": "min_not_met", | ||
"path": ["description"], |
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.
the path currently includes only the field name, we'd need to figure out how to have the full path including nesting and indexing into lists here. I've seen something here:
https://hexdocs.pm/absinthe/Absinthe.Resolution.html#path/1
but its seems to be available once the schema is resolved, which happens later in the pipeline. That said, happy to look into it separately again.
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.
- FYI don't use
path_string
- it doesn't work with list (positinos in list are integers and 😭) - we could try making it middleware rather then pipeline; there you have access to resolution
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.
FYI don't use path_string - it doesn't work with list (positinos in list are integers and 😭)
the link is indeed to path 👍
we could try making it middleware rather then pipeline; there you have access to resolution
unfortunately these errors cause early exit in the Absinthe pipeline, i.e. they never reach the Execution phase where the middlewares are called
Do you mind sharing the link to the spec @twist900? I can't find it mentioned here: https://spec.graphql.org/October2021/ |
@andreyuhai I was mostly thinking about including extensions in the errors response, please see here:
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [{ "line": 6, "column": 7 }],
"path": ["hero", "heroFriends", 1, "name"],
"extensions": {
"code": "CAN_NOT_FETCH_BY_ID",
"timestamp": "Fri Feb 9 14:33:09 UTC 2018"
}
}
]
} |
|
||
## Error Formatting | ||
|
||
The package includes GraphQL specification compliant error formatting. When enabled, validation errors from constraints or transform failures are formatted consistently. |
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.
This package is internal, isn't it? Can we link to RFC?
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.
package is public :)
"fields": [ | ||
{ | ||
"message": "min_not_met", | ||
"path": ["description"], |
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.
- FYI don't use
path_string
- it doesn't work with list (positinos in list are integers and 😭) - we could try making it middleware rather then pipeline; there you have access to resolution
|> Enum.flat_map(&format_error_group/1) | ||
end | ||
|
||
defp get_error_code(%{group_code: group_code}), do: group_code | ||
defp get_error_code(_), do: nil | ||
|
||
defp format_error_group({nil, errors}), do: errors | ||
|
||
defp format_error_group({group_code, errors}) do | ||
[ | ||
%{ | ||
message: "Invalid input", | ||
locations: [], | ||
extensions: %{ | ||
code: group_code, | ||
details: %{ | ||
fields: Enum.map(errors, &format_field/1) | ||
} | ||
} | ||
} | ||
] | ||
end |
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.
That should be idiomatic; you still would like to have one entry per group
|> Enum.flat_map(&format_error_group/1) | |
end | |
defp get_error_code(%{group_code: group_code}), do: group_code | |
defp get_error_code(_), do: nil | |
defp format_error_group({nil, errors}), do: errors | |
defp format_error_group({group_code, errors}) do | |
[ | |
%{ | |
message: "Invalid input", | |
locations: [], | |
extensions: %{ | |
code: group_code, | |
details: %{ | |
fields: Enum.map(errors, &format_field/1) | |
} | |
} | |
} | |
] | |
end | |
|> Enum.map(&format_error_group/1) | |
end | |
defp get_error_code(%{group_code: group_code}), do: group_code | |
defp get_error_code(_), do: nil | |
defp format_error_group({nil, errors}), do: errors | |
defp format_error_group({group_code, errors}) do | |
%{ | |
message: "Invalid input", | |
locations: [], | |
extensions: %{ | |
code: group_code, | |
details: %{ | |
fields: Enum.map(errors, &format_field/1) | |
} | |
} | |
} | |
end |
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.
not sure what is intended by this suggestion, should line 36 also format the errors as a single map? otherwise in one case we return a list in another a map
Have you thought about adding the changelog? 🙏 |
fd8b815
to
26d11a3
Compare
Adds error formatting phase that groups related validation errors under a single
group_error
code (e.g.BAD_USER_INPUT
), following GraphQL spec.Example Output: