2

In Angular JS web app, if my rest API returns exception (un-handled thrown) how can I display the appropriate error to the front end.

At the moment I am only able to display the generic error 'Error creating record.' but not able to retrieve the exact error message from the exception

Controller

    $http.post("/mdmservice/services/entity", $scope.entity).success(function(data, status, headers, config, statusText) {
        $scope.created=true;
        $scope.buttonsDisabled = true;          
        $scope.entityCreatedMessage = "New record created successfully.";
        $scope.error=false;
    }).error(function(data, status, headers, config, statusText ) {
        console.log("Error creating entity : " +data +"," +status +"," +headers +"," +config +"," +statusText);
        $scope.error=true;
        $scope.errorMessage="Error creating record.";
    });

REST API

    @POST
@Path(value = "/create")
@Consumes(value = MediaType.APPLICATION_JSON)
@Override
public Long create(Eentity entity) {
    Long entityId = null;
    entity.setEntityId(null);
    entity.setIsExcluded("N");
    try {
        // For create, the editIndicator=1
        entityId = entityBusiness.createOrUpdate(entity, 1);
    } catch (Exception e) {
        throw new WebApplicationException(e);
    }
    return entityId;
}

Exception log

Caused by: com.jay.MyException: Entity code must be unique
        at com.jay.BusinessImpl.createOrUpdateEntity(EntityBusinessImpl.java:93)
        at com.jay.service.impl.EntityServiceImpl.createEntity(EntityServiceImpl.java:145)
        ... 38 more
Caused by: com.jay.common.exception.EbaDataException: Entity code must be unique
        at com.jay.mdm.data.repository.jdbc.impl.EntityRepositoryJdbcImpl.createOrUpdateEntity(EntityRepositoryJdbcImpl.java:304)
        at com.jay.mdm.business.impl.EntityBusinessImpl.createOrUpdateEntity(EntityBusinessImpl.java:91)
        ... 39 more

<Dec 10, 2015 4:08:54 PM GMT> <Warning> <org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper> <BEA-000000> <javax.ws.rs.WebApplicationException: H
TTP 500 Internal Server Error       
3
  • You can access the server response for both success and error via data. $scope.entityCreatedMessage = data; Commented Dec 10, 2015 at 16:18
  • @user3632710 Thanks but the error messages are throw as an Exception and not able to get those message in the controller. Do you mean we would never throw an exception instead capture it and return a message within the returning object ? Commented Dec 10, 2015 at 16:23
  • I often use express instead of java, so I can't really help you. Maybe this question stackoverflow.com/questions/30863177/… is related to yours? Commented Dec 10, 2015 at 16:27

2 Answers 2

0

You can bind the error object to your controller, and bind it to your view to display the content of the exception, like :

$http.post("/mdmservice/services/entity", $scope.entity).success(function(data, status, headers, config, statusText) {
    $scope.created=true;
    $scope.buttonsDisabled = true;          
    $scope.entityCreatedMessage = "New record created successfully.";
    $scope.error=false;
}).error(function(data, status, headers, config, statusText ) {
    console.log("Error creating entity : " +data +"," +status +"," +headers +"," +config +"," +statusText);
    $scope.error = data;
});

In your view :

Status : {{error.statusCode}}
Label : {{error.error}}

Here is an example : http://plnkr.co/edit/UYBmz3ukMR6rhtGL6elJ?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The problem is how to get the error message from the Exception stacktrace to the controller .... inside the error block I tried to console.log all those arguments...and could not get the error message...... .error(function(data, status, headers, config, statusText ) {
You want to retrieve the Java stacktrace on the client side ? In this case you have to configure the server and make him inserts the stacktrace in the response. Sometimes that can be done by switching the server to a development mode. Then you will be able to parse the HTML returned and inject it to your controller.
0

It might help to test the API endpoint using some HTTP resource tester, and see if the error message in the stack trace appears anywhere in the response.

2 Comments

Thanks. No the exception error message does n't appear in the response. This is a thrown un-handled exception.
If it doesn't appear in the response, there's no way Angular or the client-side could get it. Find a way to include that stack trace exception in the response. Most probably, you'd have to change back-end/server-side code or configuration(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.