1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// 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); }
}
public static class Forbidden extends RestApiException {
public Forbidden(String message) { super(ErrorResponse::forbidden, message, null); }
public Forbidden(String message, Throwable cause) { super(ErrorResponse::forbidden, message, cause); }
}
}
|