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

Request errors #126

Merged
merged 8 commits into from May 1, 2019
Merged

Request errors #126

merged 8 commits into from May 1, 2019

Conversation

wadetandy
Copy link
Contributor

@wadetandy wadetandy commented Apr 16, 2019

This is the first phase of work to allow us to provide better & more
actionable errors to API users who might not also be developers of the
API itself. Previously when trying to write unwritable attributes &
relationships, on typecast problems, or trying to write an attribute
that doesn't exist, the request would blow up with a 500 and an
unhelpful error message. The request validator takes on most of the
work of transforming and validating an incoming request for processing
all while rescuing any of the above errors and turning them into helpful
error messages. Note that this is only currently intended to process
write payloads, but will be fully extendable to validate query
parameters for read requests too.

This also adds a new "payload_path" key to the meta payload for the
deserialized/normalized request payload, which allows consumers to know
which path within the request json structure they are dealing with at
any given time. Here it is used for more useful error messages.

@wadetandy
Copy link
Contributor Author

@richmolj Would like to get your feedback on the approach before I drive across the finish line. The current implementation would replace the typecasting & writability checks currently in Util::Persistence and the payload normalization currently in the runner. This would likely be integrated somewhere in the ResourceProxy or Runner layer, but open to other suggestions.

Instead of having the message change dramatically depending on whether
an AttributeError is a missing attribute or an access error, we subclass
and create a message based on the subclass.
This is the first phase of work to allow us to provide better & more
actionable errors to API users who might not also be developers of the
API itself.  Previously when trying to write unwritable attributes &
relationships, on typecast problems, or trying to write an attribute
that doesn't exist, the request would blow up with a 500 and an
unhelpful error message.  The request validator takes on most of the
work of transforming and validating an incoming request for processing
all while rescuing any of the above errors and turning them into helpful
error messages.  Note that this is only currently intended to process
write payloads, but will be fully extendable to validate query
parameters for read requests too.

This doesn't actually integrate the validator into the code path, as I
want to get feedback on the design before doing that work in case
anything needs to change.  This will take over some of the
normalization responsibilities which currently live in
`Graphiti::Util::Persistence`.

This also adds a new "payload_path" key to the meta payload for the
deserialized/normalized request payload, which allows consumers to know
which path within the request json structure they are dealing with at
any given time. Here it is used for more useful error messages.
@wadetandy wadetandy changed the title [WIP] Request errors Request errors Apr 19, 2019
@wadetandy
Copy link
Contributor Author

@richmolj This should be good to go. Going to follow up with changes to graphiti_errors later this weekend/early next week so that we can render nice errors back to API consumers.

The `Graphiti::Base` module was a legacy from jsonapi_suite that was
mixed into the `Graphiti::Runner` and `Graphiti` modules to provide them
both with functionality.  Turns out we weren't using the latter case
anymore so there's no reason for this to remain a separate concern.
Calling `.find()`, `.build()`, or `.all()` on a resource will now run
validations on the incoming params hash and raise an error if the
payload is incorrect.  It will also be easy to integrate url request
params for read requests, as we will just need to add the error handling
to the validator and they will correctly propogate.
Copy link
Contributor

@richmolj richmolj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really solid. A few tiny questions just to double-check my understanding but great PR. Let's also run through the ED sample app to sanity check before merge.

lib/graphiti/runner.rb Show resolved Hide resolved
lib/graphiti/runner.rb Outdated Show resolved Hide resolved
lib/graphiti/runner.rb Outdated Show resolved Hide resolved
@@ -180,12 +164,8 @@ def process_has_many(relationships, caller_model)
iterate(except: [:polymorphic_belongs_to, :belongs_to]) do |x|
yield x

if x[:sideload].writable?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this check happens upstream in the validator right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell this can't happen in a way that doesn't go through the runner which does the validator check. Let me know if this isn't true for any reason you're aware of.

lib/graphiti/util/simple_errors.rb Outdated Show resolved Hide resolved
wadetandy added a commit to wadetandy/graphiti that referenced this pull request Apr 30, 2019
@wadetandy
Copy link
Contributor Author

Pushed updates. Let's get graphiti_errors#5 merged and released before this is merged in.

@wadetandy wadetandy merged commit f1b301a into graphiti-api:master May 1, 2019
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.

None yet

2 participants