Handling ValidationErrorResponses


#1

Setting up a service using aurelia-fetch-client I have the following method:

public CreateEntity() : Promise<Entity>{
    return this.httpClient.fetch(route)
        .then(response => {
            if(response.ok && response.status == 200) return response.json();
        })
        .catch(err => {
            throw new Error("oh no. Something went wrong")
        })
}

Now the point is, that the API also returns validation-errors with status code of 400 and references to each invalid field.
How can I interpret the 400 error message?

In my form I currently only get the object:

this.myService.CreateEntity().then(Entity => {
    this.entity = Entity;
})

Update:
In beset case I would like to parse the error Response and work with aurelia-validation “addError”. Does anyone have an example?


#2

How can I interpret the 400 error message?

This is up to you. I recommend adding a variable on your view-model that has can hold the errors, logic in your view for viewing the errors, and then reading the errors in your catch and add them to the variable on your view model so that you can display them.

In beset case I would like to parse the error Response and work with aurelia-validation “addError”. Does anyone have an example?

I don’t sorry. I personally tend to manually handle my errors rather than rely on aurelia-validation.