diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-03-10 17:46:39 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-03-19 15:52:31 +0100 |
commit | 71b91595e873fe7b066e007a2b055788249f3418 (patch) | |
tree | 0927a8635dbdcc6733baec6a870e354803947d1f /container-core | |
parent | 42454140ec9ee5bf10de566cc01ccf342fb5ad6b (diff) |
Add exception type for restapi error responses
Diffstat (limited to 'container-core')
-rw-r--r-- | container-core/src/main/java/com/yahoo/restapi/RestApiException.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/restapi/RestApiException.java b/container-core/src/main/java/com/yahoo/restapi/RestApiException.java new file mode 100644 index 00000000000..82a4dc608c3 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/restapi/RestApiException.java @@ -0,0 +1,63 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.restapi; + +import com.yahoo.container.jdisc.HttpRequest; +import com.yahoo.container.jdisc.HttpResponse; + +import java.util.function.Function; + +/** + * A {@link RuntimeException} that represents a http response. + * + * @author bjorncs + */ +public class RestApiException extends RuntimeException { + private final int statusCode; + private final HttpResponse response; + + public RestApiException(int statusCode, String errorType, String message) { + this(new ErrorResponse(statusCode, errorType, message), message, null); + } + + public RestApiException(HttpResponse response, String message) { + this(response, message, null); + } + + public RestApiException(int statusCode, String errorType, String message, Throwable cause) { + this(new ErrorResponse(statusCode, errorType, message), message, cause); + } + + public RestApiException(HttpResponse response, String message, Throwable cause) { + super(message, cause); + this.statusCode = response.getStatus(); + this.response = response; + } + + private RestApiException(Function<String, HttpResponse> responseFromMessage, String message, Throwable cause) { + this(responseFromMessage.apply(message), message, cause); + } + + public int statusCode() { return statusCode; } + public HttpResponse response() { return response; } + + public static class NotFoundException extends RestApiException { + public NotFoundException() { super(ErrorResponse::notFoundError, "Not Found", null); } + } + + public static class MethodNotAllowed extends RestApiException { + public MethodNotAllowed() { super(ErrorResponse::methodNotAllowed, "Method not allowed", null); } + public MethodNotAllowed(HttpRequest request) { + super(ErrorResponse::methodNotAllowed, "Method '" + request.getMethod().name() + "' is not allowed", null); + } + } + + public static class BadRequest extends RestApiException { + public BadRequest(String message) { super(ErrorResponse::badRequest, message, null); } + public BadRequest(String message, Throwable cause) { super(ErrorResponse::badRequest, message, cause); } + } + + public static class InternalServerError extends RestApiException { + public InternalServerError(String message) { super(ErrorResponse::internalServerError, message, null); } + public InternalServerError(String message, Throwable cause) { super(ErrorResponse::internalServerError, message, cause); } + } +} |