4

What is the best way to customize the default error object returned by grails domain constraint validation failures?

The current JSON I am getting is

{"errors": [{"object": "com.self.learning.grails.demo.api.Person", 
    "field": "name",
    "rejected-value": "K12332434",
    "message": "Property [orgKey] of class [class com.self.learning.grails.demo.api.Person] with value [K123324343432432432432] exceeds the maximum size of [12]"
}]}

I want to get rid of "object" from the above response and want to have "error-code".

I am pretty new to grails and struggling with few basic implementations. Thanks in advance.

Karan
  • 45
  • 4

2 Answers2

2

You can create a new custom marshaller for validation errors and register it in Bootstrap.groovy as

JSON.registerObjectMarshaller( new MyCustomValidationErrorsMarshaller() )

Just replace this line by your error-code for example:

json.property( "error-code", HttpStatus.UNPROCESSABLE_ENTITY.value() )

A quick way would be to register object marshaller right in the bootstrap but that will bloat bootstrap class. It is cleaner to write a custom marshaller.

Another way would be write an interceptor and intercept response then just replace object from error response with your desired error code.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
0

You can write your own class and populate it with the data you want. Also consider including additional data you may need

Example BaseException you can use:

public class BaseException extends Exception {
    static def userService
    //import org.apache.commons.logging.LogFactory
    private static final log = LogFactory.getLog(this)

    private int status ;
    private String devMessage;
    private String extendedMessage;
    private String moreInfo;
    private int errorCode;
    boolean error = true;

    public BaseException(int status,int errorCode,String message, String extendedMessage ,String moreInfo){
        this.errorCode = errorCode;
        this.status = status;
        this.devMessage = message;
        this.extendedMessage = extendedMessage;
        this.moreInfo = moreInfo;
    }

    public JSONObject errorResponse(){
        JSONObject errorJson = new JSONObject();
        errorJson.put("status",this.status);
        errorJson.put("errorCode",this.errorCode);
        errorJson.put("message",this.devMessage);
        errorJson.put("extendedMessage",this.extendedMessage);
        errorJson.put("error",error);
        errorJson.put("dateTimeStamp", new Timestamp(new Date().time).toString());
        return errorJson;
    }

    public static BaseException createBaseException(String jsonStr) {
        try {
            def json = new JsonSlurper().parseText(jsonStr)
            return new BaseException(json["status"],json["errorCode"],json["message"], json["extendedMessage"] ,json["moreInfo"])
        } catch (Exception ex) {
            return null
        }
    }
}
Raz Abramov
  • 181
  • 12
  • Thanks for the response @Raz, but the problem here is grails domain validation failures throw `ValidationErrors` which is rendered as the one I posted in question. I can parse those errors and throw my own exception, but this doesn't seem right... i was looking whether grails provide any better way to customize the error response. – Karan Dec 31 '15 at 05:29