ember-api-actions icon indicating copy to clipboard operation
ember-api-actions copied to clipboard

Add `afterError()` callback

Open Turbo87 opened this issue 5 years ago • 1 comments

In the current situations any API errors that Ember Data reports are forwarded directly to the caller of the action as a promise rejections. If a model always wants to handle certain errors in a specific way it has to wrap the action method in another method:

_ripen: memberAction({ path: 'ripen' }),

async ripen(...args) {
  try {
    await this._ripen(...args)
  } catch (error) {
    if (error instanceof InvalidError) {
      // ...
    } else {
      throw error;
    }
  }
},

I would like to propose adding support for an afterError() callback that can decide on how to handle errors within the context of the action:

ripen: memberAction({ 
  path: 'ripen',
  afterError(error) {
    if (error instanceof InvalidError) {
      // ...
    } else {
      throw error;
    }
  }
}),

@mike-north @alexlafroscia any thoughts on this?

Turbo87 avatar Apr 16 '20 16:04 Turbo87

Sounds very reasonable to me! Makes it easy to re-use specific error-handling logic, too.

alexlafroscia avatar Apr 16 '20 16:04 alexlafroscia