Code Monkey home page Code Monkey logo

Comments (10)

taras avatar taras commented on July 30, 2024

It's because of this https://github.com/ember-cli/ember-ajax/blob/master/addon/services/ajax.js#L320. This was inherited from EmberData Adapter.

It can easily be changed, but I'm curios what should happen instead. When it's an object, should I just include the object in details or serialize it to JSON?

from ember-ajax.

cspanring avatar cspanring commented on July 30, 2024

I was just running into the same issue and resolved it for our use case by overriding the ajax service with

normalizeErrorResponse(status, headers, payload) {
  if (payload && typeof payload === 'object' && payload.errors) {
    return payload.errors;
  } else {
    return payload;
  }
}

Background: if the error payload doesn't follow the { errors: {...}} convention, I would rather see the raw payload and deal with it on the individual error handler.

from ember-ajax.

Kilowhisky avatar Kilowhisky commented on July 30, 2024

Maybe ember-ajax should switch over to what the ember-data adapters are using now? It looks like Its using a much better approach.

handleResponse(status, headers, payload, requestData) {
    if (this.isSuccess(status, headers, payload)) {
      return payload;
    } else if (this.isInvalid(status, headers, payload)) {
      return new InvalidError(payload.errors);
    }

    let errors          = this.normalizeErrorResponse(status, headers, payload);
    let detailedMessage = this.generatedDetailedMessage(status, headers, payload, requestData);

    return new AdapterError(errors, detailedMessage);
  },

https://github.com/emberjs/data/blob/master/addon/-private/adapters/rest-adapter.js#L755

from ember-ajax.

the83 avatar the83 commented on July 30, 2024

In current master, normalizeErrorResponse thankfully no longer converts the payload to a string, but I think there's another problem:

handleResponse(status, headers, payload) {
payload = payload || {};
if (this.isSuccess(status, headers, payload)) {
return payload;
} else if (this.isUnauthorizedError(status, headers, payload)) {
return new UnauthorizedError(payload.errors);
} else if (this.isForbiddenError(status, headers, payload)) {
return new ForbiddenError(payload.errors);
} else if (this.isInvalidError(status, headers, payload)) {
return new InvalidError(payload.errors);
} else if (this.isBadRequestError(status, headers, payload)) {
return new BadRequestError(payload.errors);
} else if (this.isNotFoundError(status, headers, payload)) {
return new NotFoundError(payload.errors);
} else if (this.isServerError(status, headers, payload)) {
return new ServerError(payload.errors);
}
let errors = this.normalizeErrorResponse(status, headers, payload);
return new AjaxError(errors);
}

If the error isn't a generic AjaxError (i.e. an InvalidError, ServerError, etc.), the normalizeErrorResponse function is never called. Thus if the payload doesn't have an errors key, there's no way to inspect the response from the server. normalizeErrorResponse ought to always be called prior to returning an error.

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

@the83 if you have a chance, could you check out #43 and see if that handles the problem you're running into? That changes ember-ajax to handle request errors in the same way that ember-data does. I totally see what you're saying though, the hook should be called first, and then the errors array that that returns should be passed into the other error types. I'd be happy to update #43 to fix that issue.

from ember-ajax.

the83 avatar the83 commented on July 30, 2024

@alexlafroscia that commit you just added should fix the problem. Thanks!

from ember-ajax.

alexlafroscia avatar alexlafroscia commented on July 30, 2024

@the83 awesome 👍

from ember-ajax.

AndyPyeSC avatar AndyPyeSC commented on July 30, 2024

My back end is returning a 500 code with the following response:

{
    errorMessage: ".. some description..."
}

In my .catch block, the error I receive looks like this:

{
    "message": "Ajax operation failed",
    "name": "Error",
    "errors": [
        {
            "status": "500",
            "title": "The backend responded with an error",
            "detail": "[object Object]"
        }
    ]
}

It looks like my original response is still being stringified; is this intentional? Is there anything I can do to stop this happening?

from ember-ajax.

taras avatar taras commented on July 30, 2024

@AndyPyeSC can you please try the beta release and let us know if this is still a problem.

from ember-ajax.

AndyPyeSC avatar AndyPyeSC commented on July 30, 2024

@taras Just tried it and it's working, thanks!

from ember-ajax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.