mobx-rest icon indicating copy to clipboard operation
mobx-rest copied to clipboard

Save operation does not set errors

Open ax487 opened this issue 8 years ago • 4 comments

I would appreciate it if you could look into this:

I try to save a model using model.save:

model.save().then((result) => {
  console.log('Save operation finished')
  console.log('Result: ')
  console.log(result)
  console.log('Model:')
  console.log(model)
  console.log('Error in model:')
  console.log(model.error)
})

The server returns a 422 (Unprocessable Entity) error. I see the following:

  • The result contains the JSON error from the server.
  • The model has previously edited values set.
  • The model.error is null

The last point is obviously a problem. Would you consider this a bug? If this is intended, how is it possible to spot the error?

ax487 avatar Jun 04 '17 08:06 ax487

I don't know how to reproduce this. A 422 should halt the save operation and set the payload of the server response as errors. Could you write a failing test for that scenario? Thanks in advance!

masylum avatar Jun 05 '17 09:06 masylum

I ran into this as well. The problem is that the adapters expect that the server returns the errors in the errors key.

https://github.com/masylum/mobx-rest-fetch-adapter/blob/master/src/index.js#L52 https://github.com/masylum/mobx-rest-jquery-adapter/blob/master/src/index.js#L92

I think the adapters shouldn't assume anything regarding to the backend response, or al least we should have some way to customize how to extract the error from the response.

Edit: Actually it wasn't the same scenario. In my case the promise got rejected but I couldn't get the error payload.

rdiazv avatar Jun 05 '17 17:06 rdiazv

I think this was the cause https://github.com/masylum/mobx-rest-fetch-adapter/pull/2. @ax487, were you using the fetch adapter?

rdiazv avatar Jun 27 '17 16:06 rdiazv

I think that was the case. We could consider though to remove the assumption that the response always includes an errors key, or at least make it configurable (for backward compatibility).

masylum avatar Jun 28 '17 11:06 masylum