Files
matrix/cygnus-lib/src/main/java/lib/models/ErrorDetails.java

59 lines
1.9 KiB
Java

package lib.models;
import lib.constants.ApplicationError;
import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
import lombok.Getter;
/** Dependency-free error information shared across Cygnus modules. */
@Getter
public final class ErrorDetails implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private final int httpStatus;
private final String errorCode;
private final String title;
private final String message;
private final String description;
private final String referenceId;
private final Instant timestamp;
public ErrorDetails(ApplicationError error) {
this(error.getHttpStatus(), error.getCode(), error.getTitle(),
error.getMessage(), error.getDescription(),
UUID.randomUUID().toString(), Instant.now());
}
private ErrorDetails(
int httpStatus,
String errorCode,
String title,
String message,
String description,
String referenceId,
Instant timestamp) {
if (httpStatus < 400 || httpStatus > 599) {
throw new IllegalArgumentException("HTTP error status must be between 400 and 599");
}
this.httpStatus = httpStatus;
this.errorCode = required(errorCode, "errorCode");
this.title = required(title, "title");
this.message = required(message, "message");
this.description = description == null ? "" : description.trim();
this.referenceId = required(referenceId, "referenceId");
this.timestamp = Objects.requireNonNull(timestamp, "timestamp");
}
private static String required(String value, String name) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException(name + " is required");
}
return value.trim();
}
}