feat: add exception

This commit is contained in:
2026-08-16 20:08:25 +07:00
parent 0c4e4adac8
commit 970e91ddc5
4 changed files with 79 additions and 0 deletions
@@ -0,0 +1,15 @@
package aplp.backend.core.common.exception;
public class DomainException extends RuntimeException {
private final ErrorCode code;
public DomainException(ErrorCode code, String message) {
super(message);
this.code = code;
}
public ErrorCode code() {
return code;
}
}
@@ -0,0 +1,26 @@
package aplp.backend.core.common.exception;
public enum ErrorCode {
INVALID_ARGUMENT(400),
VALIDATION_FAILED(400),
MALFORMED_REQUEST(400),
UNAUTHENTICATED(401),
INVALID_CREDENTIALS(401),
INVALID_REFRESH_TOKEN(401),
ACCESS_DENIED(403),
EMAIL_ALREADY_EXISTS(409),
USER_NOT_FOUND(404),
LEARNER_NOT_FOUND(404),
INTERNAL_ERROR(500);
private final int status;
ErrorCode(int status) {
this.status = status;
}
public int status() {
return status;
}
}
@@ -0,0 +1,17 @@
package aplp.backend.core.common.response;
import java.time.Instant;
public record ApiError(
Instant timestamp,
int status,
String error,
String code,
String message,
String traceId
) {
public static ApiError of(int status, String error, String code, String message, String traceId) {
return new ApiError(Instant.now(), status, error, code, message, traceId);
}
}
@@ -0,0 +1,21 @@
package aplp.backend.core.common.util;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
public final class TokenHasher {
private TokenHasher() {
}
public static String sha256Hex(String value) {
try {
byte[] digest = MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8));
return HexFormat.of().formatHex(digest);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 not available", e);
}
}
}