-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Friendly Errors #101
Closed
Closed
Friendly Errors #101
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
- Start Date: 2015-10-23 | ||
- RFC PR: (leave this empty) | ||
- Ember Issue: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add more illustrative detail to the default Ember Data Adapter Errors. | ||
|
||
# Motivation | ||
|
||
With a production Ember project, it's common to have many errors of the form "Adapter Error", | ||
originating from deep in the Ember Data stack and carrying little context about what the | ||
original error cause was. | ||
|
||
The intent is to add the original request URL, the response code, and some payload information | ||
to the default Error message for `DS.AdapterError`s. From there Errors can be handled or | ||
tracked as normal. | ||
|
||
# Detailed design | ||
|
||
I've been using something similar to the following Adapter (`friendly-error-adapter.js`): | ||
|
||
```js | ||
import ActiveModelAdapter from 'active-model-adapter'; | ||
|
||
import DS from 'ember-data'; | ||
|
||
export default ActiveModelAdapter.extend({ | ||
|
||
ajax(url, method) { | ||
this.lastRequest = { | ||
url: url, | ||
method: method | ||
}; | ||
return this._super(...arguments); | ||
}, | ||
|
||
handleResponse: function (status, headers, payload) { | ||
let payloadContentType = headers["Content-Type"].split(";").get("firstObject"); | ||
let shortenedPayload; | ||
|
||
if (payloadContentType === "text/html" && payload.length > 250) { | ||
shortenedPayload = "[omitted long blob of HTML]"; | ||
} else { | ||
shortenedPayload = payload; | ||
} | ||
|
||
let errorMessage = `Ember Data Error (${this.lastRequest.method} ${this.lastRequest.url} returned a ${status}). \n Payload (${payloadContentType}): \n\n ${shortenedPayload}`; | ||
|
||
if (this.isSuccess(status, headers, payload)) { | ||
return payload; | ||
} else if (this.isInvalid(status, headers, payload)) { | ||
return new DS.InvalidError(payload.errors, errorMessage); | ||
} | ||
|
||
let errors = this.normalizeErrorResponse(status, headers, payload); | ||
|
||
return new DS.AdapterError(errors, errorMessage); | ||
} | ||
}); | ||
``` | ||
|
||
(Note that the code inside the adapter could be MUCH simpler and cleaner, the above | ||
is a very quick hacked up example! :bomb:) | ||
|
||
The intent is to get an error message out of the form: | ||
|
||
1. "Ember Data Error" | ||
2. Request Method & URI | ||
3. Response Status | ||
4. Response Content Type | ||
5. A sane representation of the Response payload | ||
|
||
# Drawbacks | ||
|
||
Adding complexity to an Error handler always runs the risk of generating errors inside | ||
the handler itself, which would not be overly friendly. | ||
|
||
# Alternatives | ||
|
||
There's probably quite a few different pieces of information that could be included | ||
in the message. | ||
|
||
We could also potentially look at attaching the extra information to other fields on | ||
the `AdapterError` (and its subclasses). The only drawback there would be that most | ||
error reporters would then not include that information by default. | ||
|
||
# Unresolved questions | ||
|
||
* Exact Error Message Format |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 technique may not work with multiple concurrent ajax requests. Instead I suspect we will need to pass the request or errorMessage as an argument to
handleResponse
.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.
Yeah, mostly a hack to avoid trying to drop too deeply into the
ember-data
code. Definitely better ways to do it!