@Getter
@ToString
@With
@AllArgsConstructor(onConstructor_ = {@JsonCreator})
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ApiError {
/**
* A machine-readable representation of the type of error being returned
*/
private final String type;
/**
* A human-readable representation of the type of error being returned
*/
private final String title;
/**
* Optional exception class name
*/
@Setter
private String exception;
/**
* When the error occurred, defaults to {@link LocalDateTime#now()}
*/
@Setter
private OffsetDateTime timestamp = Instant.now().atOffset(ZoneOffset.UTC);
/**
* Should always match exactly the status in the response header. Makes it easier to parse out
* and log
*/
private final HttpStatus status;
/**
* Numerical representation of {@link #status}
*/
private final int statusCode;
/**
* Optionally gives additional information to the client about what was wrong with their request
*
* @see #addError(GlobalError)
*/
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
private List<GlobalError> globalErrors = new ArrayList<>();
/**
* Initializes a new error
*
* @param type a machine-readable representation of this error
* @param title a human-readable representation of the error
* @param status HTTP status
*/
public ApiError(String type, String title, @NonNull HttpStatus status) {
this.type = type;
this.title = title;
this.status = status;
this.statusCode = status.value();
}
public ResponseEntity<ApiError> toResponseEntity() {
return new ResponseEntity<>(this, HttpStatus.valueOf(statusCode));
}
/**
* Gives more information to a client about what went wrong in their request
*
* @param error the error to add to this response
*/
public void addError(GlobalError error) {
globalErrors.add(error);
}
}