1

I have a class like that:

@Override
public StudentDTO getStudent(@WebParam(name = "name") String studentName) {
    StudentDTO student = new StudentDTO();
    try {
        student = studentService.findStudentByName(studentName);
    } catch (Exception e) {
        return new ErrorActionResponse("student couldn't find by name");
    }
    return student;
}

As usual this doesn't work because of return type is StudentDTO and I try to return another type of class: ErrorActionResponse. ErrorActionResponse is an error class that has detailed information about error.

How can I design my web service architecture that can handle error situations? (At my REST architecture I write error information into response and send error to client side)

2 Answers 2

1

If you want to return a Collection (as stated in a comment to my earlier answer), I suggest you create a Map with two keys. If there is no exception, first key-value pair will contain "students" string and StudentDTO collection, respectively. And, second key-value pair will contain "exception" string and null value, respectively. If there is an exception, first key-value pair will contain "students" string and null value, respectively. And, second key-value pair will be "exception" string and an ErrorActionResponse object, respectively. Example:

No exception case:

Map<String, List> result = new HashMap<String, List>();
result.put( "students", COLLECTION_OF_STUDENTS );
result.put( "exception", null );

No exception case:

Map<String, List> result = new HashMap<String, List>();
result.put( "students", null );
result.put( "exception", ErrorActionResponse_OBJECT );

Hope this helps...

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

Comments

1

For least impact I suggest: Make ErrorActionResponse as a private member of StudentDTO with setter and getter methods. In service, when there's an exception, instantiate ErrorActionResponse and set same in StudentDTO's member. So, client has to first check if getErrorActionResponse() returns null. If so, do normal processing else, handle exception case.

CLASS StudentDTO:

public class StudentDTO {

    ...
    private ErrorActionResponse errorActionResponse;
    ...

    public ErrorActionResponse getErrorActionResponse() {
        return errorActionResponse;
    }

    public void setErrorActionResponse( ErrorActionResponse errorActionResponse ) {
        this.errorActionResponse = errorActionResponse;
    }

}

SERVICE:

@Override
public StudentDTO getStudent(@WebParam(name = "name") String studentName) {
    StudentDTO student = new StudentDTO();
    try {
        student = studentService.findStudentByName(studentName);
    } 
    catch (Exception e) {
        student.setErrorActionResponse( new ErrorActionResponse("student couldn't find by name") );
    }
    finally {
        return student;
    }
}

CLIENT CODE:

if( student.getErrorActionResponse() == null ) {
    // do normal processing
}
else {
    // handle exception case
}

In the above case, DTO has ErrorActionResponse member which is not related to it's fundamental state. So, for cleaner approach, I suggest you to consider Adapter pattern.

2 Comments

If I try to return list of students I will need to set all student's errorActionResponse at the list
I agree. My first suggestion was for your OP requirement - method that returns a student and not a list (which you are stating now).

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.