Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fine-grained HTTP error exceptions with client and server errors #854

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 54 additions & 17 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ public static FeignException errorStatus(String methodKey, Response response) {
}

private static FeignException errorStatus(int status, String message, byte[] body) {
if (isClientError(status)) {
return clientErrorStatus(status, message, body);
}
if (isServerError(status)) {
return serverErrorStatus(status, message, body);
}
return new FeignException(status, message, body);
}

private static boolean isClientError(int status) {
return status >= 400 && status < 500;
}

private static FeignClientException clientErrorStatus(int status, String message, byte[] body) {
switch (status) {
case 400:
return new BadRequest(message, body);
Expand All @@ -110,6 +124,17 @@ private static FeignException errorStatus(int status, String message, byte[] bod
return new TooManyRequests(message, body);
case 422:
return new UnprocessableEntity(message, body);
default:
return new FeignClientException(status, message, body);
}
}

private static boolean isServerError(int status) {
return status >= 500 && status <= 599;
}

private static FeignServerException serverErrorStatus(int status, String message, byte[] body) {
switch (status) {
case 500:
return new InternalServerError(message, body);
case 501:
Expand All @@ -121,7 +146,7 @@ private static FeignException errorStatus(int status, String message, byte[] bod
case 504:
return new GatewayTimeout(message, body);
default:
return new FeignException(status, message, body);
return new FeignServerException(status, message, body);
}
}

Expand All @@ -134,97 +159,109 @@ static FeignException errorExecuting(Request request, IOException cause) {
null);
}

public static class BadRequest extends FeignException {
public static class FeignClientException extends FeignException {
public FeignClientException(int status, String message, byte[] body) {
super(status, message, body);
}
}

public static class BadRequest extends FeignClientException {
public BadRequest(String message, byte[] body) {
super(400, message, body);
}
}

public static class Unauthorized extends FeignException {
public static class Unauthorized extends FeignClientException {
public Unauthorized(String message, byte[] body) {
super(401, message, body);
}
}

public static class Forbidden extends FeignException {
public static class Forbidden extends FeignClientException {
public Forbidden(String message, byte[] body) {
super(403, message, body);
}
}

public static class NotFound extends FeignException {
public static class NotFound extends FeignClientException {
public NotFound(String message, byte[] body) {
super(404, message, body);
}
}

public static class MethodNotAllowed extends FeignException {
public static class MethodNotAllowed extends FeignClientException {
public MethodNotAllowed(String message, byte[] body) {
super(405, message, body);
}
}

public static class NotAcceptable extends FeignException {
public static class NotAcceptable extends FeignClientException {
public NotAcceptable(String message, byte[] body) {
super(406, message, body);
}
}

public static class Conflict extends FeignException {
public static class Conflict extends FeignClientException {
public Conflict(String message, byte[] body) {
super(409, message, body);
}
}

public static class Gone extends FeignException {
public static class Gone extends FeignClientException {
public Gone(String message, byte[] body) {
super(410, message, body);
}
}

public static class UnsupportedMediaType extends FeignException {
public static class UnsupportedMediaType extends FeignClientException {
public UnsupportedMediaType(String message, byte[] body) {
super(415, message, body);
}
}

public static class TooManyRequests extends FeignException {
public static class TooManyRequests extends FeignClientException {
public TooManyRequests(String message, byte[] body) {
super(429, message, body);
}
}

public static class UnprocessableEntity extends FeignException {
public static class UnprocessableEntity extends FeignClientException {
public UnprocessableEntity(String message, byte[] body) {
super(422, message, body);
}
}

public static class InternalServerError extends FeignException {
public static class FeignServerException extends FeignException {
public FeignServerException(int status, String message, byte[] body) {
super(status, message, body);
}
}

public static class InternalServerError extends FeignServerException {
public InternalServerError(String message, byte[] body) {
super(500, message, body);
}
}

public static class NotImplemented extends FeignException {
public static class NotImplemented extends FeignServerException {
public NotImplemented(String message, byte[] body) {
super(501, message, body);
}
}

public static class BadGateway extends FeignException {
public static class BadGateway extends FeignServerException {
public BadGateway(String message, byte[] body) {
super(502, message, body);
}
}

public static class ServiceUnavailable extends FeignException {
public static class ServiceUnavailable extends FeignServerException {
public ServiceUnavailable(String message, byte[] body) {
super(503, message, body);
}
}

public static class GatewayTimeout extends FeignException {
public static class GatewayTimeout extends FeignServerException {
public GatewayTimeout(String message, byte[] body) {
super(504, message, body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public static Object[][] errorCodes() {
{409, FeignException.Conflict.class},
{429, FeignException.TooManyRequests.class},
{422, FeignException.UnprocessableEntity.class},
{450, FeignException.FeignClientException.class},
{500, FeignException.InternalServerError.class},
{501, FeignException.NotImplemented.class},
{502, FeignException.BadGateway.class},
{503, FeignException.ServiceUnavailable.class},
{504, FeignException.GatewayTimeout.class},
{599, FeignException.FeignServerException.class},
{599, FeignException.class},
};
}
Expand Down