Data: How do you enable extended errors?

Created on 29 Dec 2016  Â·  17Comments  Â·  Source: emberjs/data

Hi,

I am using ember-cli-2.9.1 and I need to use extended errors like DS.UnauthorizedError and the like. How do I enable these? I don't see any documentation and I can't get these errors to be recognized ...

I have tried the following in my config/environment.js but none of them work (based on [errors.js]((https://github.com/emberjs/data/blob/v2.9.0/addon/adapters/errors.js):

    EmberENV: {
      FEATURES: {
        // Warns about canary  builds but does nothing ...
        'ds-extended-errors': true,
        // Same ...
        'extended-errors': true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false,
        // Does nothing
        'ds-extended-errors': true
        // Does nothing
        'extended-errors': true
      }

It's not at all clear to me how to turn on these errors. Are these still in private beta? If so, what's the ETA to fully release them? If not, how do I turn them on?

Having special error classes for 401s etc seems pretty critical to me - I want to show custom error messages to my user based on different error cases. So in my notification service I have a switch like the following:

  saveError(error, context, suggestion) {
    let explanation = null;
    if (error instanceof DS.InvalidError) {
      explanation = `it had invalid data.  ${suggestion}`;
    } else if (error instanceof DS.TimeoutError) {
      explanation = "the request to our server timed out - please check your network connection and try again.";
    } else if (error instanceof DS.AbortError) {
      explanation = "something went wrong in your browser - please try again or report this error to Presence Labs.";
    } else if (error instanceof DS.UnauthorizedError) {
      // Causes error in Javascript: ember.debug.js:19160 TypeError: Right-hand side of 'instanceof' is not an object
      explanation = "you are not authorized to do this - please try logging out and logging again or talk to your organization's admin.";
    } else if (error instanceof DS.ForbiddenError) {
      explanation = "you are not allowed to do this - please talk to your organization's admin and ask for permission to do this.";
    } else if (error instanceof DS.NotFoundError) {
      explanation = "the data you're trying to update no longer exists on the server - maybe somebody deleted something?  Please try again.";
    } else if (error instanceof DS.ConflictError) {
      explanation = "there was conflicting data that we couldn't resolve.  Please try again.";
    } else if (error instanceof DS.ServerError) {
      explanation = "there seems to be something wrong with the Presence Labs server - please try again or contact us about this problem.";
    } else {
      explanation = `unknown error occurred: ${error}.`;
    }

    let message = `We're sorry, we were unable to ${context} because ${explanation}`;

    this.error(message);
  }

Any help is hugely appreciated! I'm happy to submit a PR for improved documentation if somebody can help me get this working ... :-)

Most helpful comment

I'm still getting the error UnauthorizedError is not a constructor with Ember 2.14 / Ember Data 2.14.3, so is this usable with these versions or not yet? I can't figure out why this is not working if this is supposed to be working since 2.13.

All 17 comments

@joelpresence are you on a canary/beta channel that includes this feature flag?

No, I'm on the production/release version of ember-cli-2.9.1 and I need to use extended errors there. How can I do that? I can't use a beta build ... is there an ETA for when these extended errors will be fully released to production? It's pretty important that we be able to message different errors differently to users. 😀

Alternatively, I could access the http status code and message directly myself but I don't see any documentation on how to do that. I'm using the JSONAPIAdapter. Any suggestions?

Thanks!

Sent from my iPhone

On Dec 29, 2016, at 4:50 AM, Ricardo Mendes <[email protected]notifications@github.com> wrote:

@joelpresencehttps://github.com/joelpresence are you on a canary/beta channel that includes this feature flag?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/emberjs/data/issues/4732#issuecomment-269626041, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALf8WRLmHZ30IsemRZQ7D785ewMXO1dtks5rM6x8gaJpZM4LXQKb.

Bump ... any ideas? Thanks!

Same problem here. I followed this guide http://emberjs.com/api/data/classes/DS.UnauthorizedError.html but was not able to set it up. There is not mentioning, that a feature needs to be enabled for some errors (even though there seems still to be a condition in the DS build process). Tried with and without feature enabled. No luck.

Also those documentations don't mention that you need to call return true; in route.actions.error to execute the default behaviour.

me too =(

+1

Feature flags are only available on canary builds. You would need to create a temporary fork of ember data and enable the feature flag in the forked repo to get this feature.

This took me some head scratching. The official docs for 2.11 describe using this feature but was completely taken aback when the example all returned null. To a newbie this can be very upsetting. Can documentation be in sync with stable releases with a canary version of the docs elsewhere?

@NLincoln you can set the flags in your app's environment.

@sukima they should be, if a feature flag documentation is showing there might have been an annotation lapse.

It is in the documentation for 2.11 with no mention of a feature flag that I could see. And yet it seems to not work in 2.11 (probably because of the feature flag): https://ember-twiddle.com/b299e3261926522b0996b4c70ff4af32

Yeah still not in 2.11 (neither in 2.12.0-beta.1) and I needed it. So I ended up creating (well... copying, see below) a helper which I call to extend my errors, and in case the .extend() exists it'll just call that "native" one:

function extend(ParentErrorClass, defaultMessage) {
  if (ParentErrorClass.extend) {
    return ParentErrorClass.extend({ message: defaultMessage });
  }
  let ErrorClass = function (errors, message) {
    assert('`AdapterError` expects json-api formatted errors array.', Array.isArray(errors || []));
    ParentErrorClass.call(this, errors, message || defaultMessage);
  };
  ErrorClass.prototype = Object.create(ParentErrorClass.prototype);

  return ErrorClass;
}

which I can use like this: extend(DS.InvalidError, 'My custom default error message')

It's basically almost a copy/paste from what ember data uses in its sources: https://github.com/emberjs/data/blob/v2.11.0/addon/adapters/errors.js#L101-L113 ;-)

it seems this is not available in 2.12.1 either? any timeline on this?

ds-extended-errors is enabled in the v2.13.0 beta branch if all goes well it will be included with the v2.13.0 release.

Hi, I'm using the lasted version of ember-data, does it also need to upgrade ember-cli to 2.13?

If I just upgrade ember and ember-data to 2.13, it seems that this feature is still not enabled, the compiled code(ember-data/adapters/errors) looks like this:

var extendedErrorsEnabled = false;
if ((0, _emberDataPrivateFeatures['default'])('ds-extended-errors')) {
  extendedErrorsEnabled = true;
}

But if I upgrade the ember-cli at the same time, the compiled code looks like this:

var extendedErrorsEnabled = false;
if (true) {
  extendedErrorsEnabled = true;
}

Is it right? Do I missed something?

We are using Ember 2.12 / Ember Data 2.11 - so DS.UnauthorizedError() is available in 2.13 and above?

I'm getting an error that UnauthorizedError is not a constructor

I'm still getting the error UnauthorizedError is not a constructor with Ember 2.14 / Ember Data 2.14.3, so is this usable with these versions or not yet? I can't figure out why this is not working if this is supposed to be working since 2.13.

There has been a bug with enabling this feature, but it has been fixed in #5191. This should work now as expected on the latest release, see this ember-twiddle.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bartocc picture bartocc  Â·  4Comments

toobulkeh picture toobulkeh  Â·  3Comments

bekicot picture bekicot  Â·  4Comments

dknutsen picture dknutsen  Â·  4Comments

HenryVonfire picture HenryVonfire  Â·  3Comments