Design for Error Response in Web Services

The server side should send error code and error message.  Is that all? 

Yes, basically it will work, but not perfect yet.

What you need to do:

1. Differentiate known, user-readable errors from unknown, developer-readable errors.  

2. Send an error ID to client side when there is an unknown error, with which the client developer can help server side developer locate the error’s detailed information, thus more easily troubleshoot the problem. 

An example in Java:

public abstract class Err {

	protected String errCode;
public class KnownErr extends RestErr {
	private String errMsg;
public class UnknownErr extends Err {

	private String devErrMsg;
	//the error ticket
	private String exceptionId;

//You must have had a global exception handler. Do something like, 

		String exceptionId = Thread.currentThread().getName() + "-"
				+ DateFormatUtils.format(new Date(), "yyyyMMddHHmmsSSS") + "-"
				+ RandomStringUtils.randomAlphanumeric(5);

		// first log it
		logger.error("Unknown Server Error. Exception Id = "
				+ exceptionId, ex);

		UnknownErr err = new UnknownErr(someErrCode, exceptionNameAsDevErrMsg, exceptionId);



Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.