Error / exception interception in REST

Hi,
I want to throw an exception from my services so in the client(website) ill be able to see something similar to cuba:

{
   "error" : "country_not_exists",
   "error_description" : "Country not exists - Choose another"
}

Can I intercept the errors, or should I throw particular error to acheive that?
Thanks

You can start with com.haulmont.cuba.core.global.validation.CustomValidationException and throw it from your service code. It is translated to JSON errors automatically.

@artamonov thanks, but ValidationException is not so helpfull, some time you need to pass the property name of the field. or maybe other params such as dependency field…
What is the apropriate way to create my own custom exception that will be translated automatically to json with status code?

I have tried to extend CustomValidationException:

@SupportedByClient
public class WebException extends CustomValidationException {
    private String errorCode;
    private String errorDescription;
   
    public WebException(){
        super("");
    }

    public  WebException(String errorCode){
        super(errorCode);
        this.errorCode = errorCode;
    }
    public WebException(String errorCode, String errorDescription){
        super(errorCode);
        this.errorCode = errorCode;
        this.errorDescription = errorDescription;
    }

    //.. getters setters
}

But it is not serialized…
Thanks again!

B.T.W throwing CustomValidationException from the service not serialized as well

Take a look at this topic