package org.resthub.common.model; import org.springframework.util.ObjectUtils; /** * Value object use to serialize REST error messages * @author Les Hazlewood */ public class RestError { /** HTTP status reason phrase, form example "Internal Server Error" **/ private final String status; /** HTTP status code **/ private final int code; /** Message for end users **/ private final String message; /** Message for developers **/ private final String developerMessage; /** Eventually an URL where we can get more information about the error **/ private final String moreInfoUrl; /** The exception thrown **/ private final Throwable throwable; public RestError(String status, int code, String message, String developerMessage, String moreInfoUrl, Throwable throwable) { if (status == null) { throw new NullPointerException("HttpStatus argument cannot be null."); } this.status = status; this.code = code; this.message = message; this.developerMessage = developerMessage; this.moreInfoUrl = moreInfoUrl; this.throwable = throwable; } public String getStatus() { return status; } public int getCode() { return code; } public String getMessage() { return message; } public String getDeveloperMessage() { return developerMessage; } public String getMoreInfoUrl() { return moreInfoUrl; } public Throwable getThrowable() { return throwable; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof RestError) { RestError re = (RestError) o; return ObjectUtils.nullSafeEquals(getStatus(), re.getStatus()) && getCode() == re.getCode() && ObjectUtils.nullSafeEquals(getMessage(), re.getMessage()) && ObjectUtils.nullSafeEquals(getDeveloperMessage(), re.getDeveloperMessage()) && ObjectUtils.nullSafeEquals(getMoreInfoUrl(), re.getMoreInfoUrl()) && ObjectUtils.nullSafeEquals(getThrowable(), re.getThrowable()); } return false; } @Override public int hashCode() { //noinspection ThrowableResultOfMethodCallIgnored return ObjectUtils.nullSafeHashCode(new Object[]{ getStatus(), getCode(), getMessage(), getDeveloperMessage(), getMoreInfoUrl(), getThrowable() }); } public String toString() { //noinspection StringBufferReplaceableByString return new StringBuilder().append(getCode()) .append(" (").append(getStatus()).append(" )") .toString(); } public static class Builder { private String status; private int code; private String message; private String developerMessage; private String moreInfoUrl; private Throwable throwable; public Builder() { } public Builder setStatus(String status) { this.status = status; return this; } public Builder setCode(int code) { this.code = code; return this; } public Builder setMessage(String message) { this.message = message; return this; } public Builder setDeveloperMessage(String developerMessage) { this.developerMessage = developerMessage; return this; } public Builder setMoreInfoUrl(String moreInfoUrl) { this.moreInfoUrl = moreInfoUrl; return this; } public Builder setThrowable(Throwable throwable) { this.throwable = throwable; return this; } public RestError build() { if (this.status == null) { this.status = "Internal Server Error"; } return new RestError(this.status, this.code, this.message, this.developerMessage, this.moreInfoUrl, this.throwable); } } }