-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
[DATA] Deprecate extending Native Error (AdapterError and subclasses) #402
Comments
@runspired this is the list of errors I see in the docs (3.9) and a few example uses ... DS.AdapterError docs Subclasses:
Various uses... Custom error extending import DS from 'ember-data';
export default DS.AdapterError.extend({ message: "Down for maintenance." }); new MaintenanceError(); Condition to match error type error instanceof TimeoutError So we'd want to keep the above behavior, and no longer extend Also we'd want to use typical JS custom errors, Error#Custom_Error_Types Properties we'd add to the Adapter Errors:
Properties based on Error prototype...
Existing source for AdapterError Perhaps using class AdapterError extends Error {
constructor(errors = [{title: 'AdapterError', detail: 'Adapter operation failed'}]) {
super();
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AdapterError);
}
this.message = 'Adapter operation failed',
this.name = 'AdapterError';
this.errors = errors;
}
static extend(props) {
// logic for compatibility and deprecation warning
}
}
class NotFoundError extends AdapterError {
constructor(errors = [{title: 'Not Found', detail: 'Resource could not be found', status: '404'}]) {
super(errors);
this.message = 'Not Found';
this.name = 'NotFoundError';
this.code = 404;
}
static extend(props) {
// logic for compatibility and deprecation warning
}
} I think it's important to keep support for type comparisons |
@pixelhandler there's a couple of driving motivations for this but I'll list the biggest:
Today we have to write very convoluted extension patterns that decrease the legibility of the errors printed to the console because the following does not work cross-platform (and may never) class AdapterError extends Error {
constructor() {
super(...arguments);
this.isAdapterError = true;
}
} Moving to a functional pattern is beneficial for performance and maintainability. An example API might be: function createAdapterError(errors, message, statusCode, statusText) {
const error = new Error(message);
Object.assign(error, {
isAdapterError: true,
statusCode,
statusText,
errors
});
return error;
}
function is500Error(error) {
return error.isAdapterError && error.statusCode === 500;
} While at first the functional approach may seem to diminish value because we lose the
|
This quirk is better deprecated by the broader deprecation of the adapter/serializer pattern that will occur once RequestManager is moved to the recommended stage |
Alternative API would be something like
isAdapterError(thing)
andbuildAdapterError(options)
, the only real requirement is theerrors
array on theError
, and possibly a property to specify it as anadapterError
. All current subclasses could implement some form ofcode
ortype
in addition to their custom message, but we would only ever use native errors. alaThe text was updated successfully, but these errors were encountered: