-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from exacaster/unify_error_responses
Unify error responses
- Loading branch information
Showing
15 changed files
with
203 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
server/src/main/java/com/exacaster/lighter/application/sessions/StatementCreationResult.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
...c/main/java/com/exacaster/lighter/application/sessions/StatementCreationResultMapper.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
...a/com/exacaster/lighter/application/sessions/exceptions/InvalidSessionStateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.exacaster.lighter.application.sessions.exceptions; | ||
|
||
import com.exacaster.lighter.application.ApplicationState; | ||
|
||
public class InvalidSessionStateException extends RuntimeException { | ||
private final ApplicationState sessionState; | ||
|
||
public InvalidSessionStateException(ApplicationState state) { | ||
super("Invalid session state: " + state); | ||
this.sessionState = state; | ||
} | ||
|
||
public ApplicationState getSessionState() { | ||
return sessionState; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "InvalidSessionStateException{" + | ||
"sessionState=" + sessionState + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
server/src/main/java/com/exacaster/lighter/rest/exceptions/ApiErrorResponseProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.exacaster.lighter.rest.exceptions; | ||
|
||
import io.micronaut.context.annotation.Primary; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.http.HttpMethod; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.MutableHttpResponse; | ||
import io.micronaut.http.server.exceptions.response.ErrorContext; | ||
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor; | ||
|
||
import javax.inject.Singleton; | ||
|
||
@Primary | ||
@Singleton | ||
public class ApiErrorResponseProcessor implements ErrorResponseProcessor<ErrorResponse> { | ||
@NonNull | ||
@Override | ||
public MutableHttpResponse<ErrorResponse> processResponse(@NonNull ErrorContext errorContext, | ||
@NonNull MutableHttpResponse response) { | ||
if (errorContext.getRequest().getMethod() == HttpMethod.HEAD) { | ||
return (MutableHttpResponse<ErrorResponse>) response; | ||
} | ||
var path = errorContext.getRequest().getUri(); | ||
var error = new ErrorResponse(response.getStatus().getReason(), path, errorContext.getErrors()); | ||
|
||
return response.body(error).contentType(MediaType.APPLICATION_JSON_TYPE); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
server/src/main/java/com/exacaster/lighter/rest/exceptions/DetailedError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.exacaster.lighter.rest.exceptions; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.http.server.exceptions.response.Error; | ||
|
||
@Introspected | ||
public class DetailedError implements Error { | ||
|
||
private final String message; | ||
|
||
private final Object details; | ||
|
||
public DetailedError(String message, Object details) { | ||
this.message = message; | ||
this.details = details; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public Object getDetails() { | ||
return details; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DetailedError{" + | ||
"message='" + message + '\'' + | ||
", details=" + details + | ||
'}'; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
server/src/main/java/com/exacaster/lighter/rest/exceptions/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.exacaster.lighter.rest.exceptions; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.http.server.exceptions.response.Error; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@Introspected | ||
public class ErrorResponse { | ||
private final String message; | ||
private final URI path; | ||
private final List<Error> errors; | ||
|
||
public ErrorResponse(String message, URI path, List<Error> errors) { | ||
this.message = message; | ||
this.path = path; | ||
this.errors = errors; | ||
} | ||
|
||
public ErrorResponse(String message, URI path) { | ||
this(message, path, List.of()); | ||
} | ||
|
||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public URI getPath() { | ||
return path; | ||
} | ||
|
||
public List<Error> getErrors() { | ||
return errors; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ErrorResponse{" + | ||
"message='" + message + '\'' + | ||
", path=" + path + | ||
", errors=" + errors + | ||
'}'; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../main/java/com/exacaster/lighter/rest/exceptions/InvalidSessionStateExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.exacaster.lighter.rest.exceptions; | ||
|
||
import com.exacaster.lighter.application.sessions.exceptions.InvalidSessionStateException; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpResponse; | ||
import io.micronaut.http.MutableHttpResponse; | ||
import io.micronaut.http.annotation.Produces; | ||
import io.micronaut.http.server.exceptions.ExceptionHandler; | ||
import io.micronaut.http.server.exceptions.response.ErrorContext; | ||
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Produces | ||
@Singleton | ||
@Requires(classes = InvalidSessionStateException.class) | ||
public class InvalidSessionStateExceptionHandler implements ExceptionHandler<InvalidSessionStateException, HttpResponse<?>> { | ||
|
||
private final ErrorResponseProcessor<?> responseProcessor; | ||
|
||
@Inject | ||
public InvalidSessionStateExceptionHandler(ErrorResponseProcessor<?> responseProcessor) { | ||
this.responseProcessor = responseProcessor; | ||
} | ||
|
||
@Override | ||
public HttpResponse<?> handle(HttpRequest request, InvalidSessionStateException exception) { | ||
final ErrorContext.Builder contextBuilder = ErrorContext.builder(request).cause(exception); | ||
MutableHttpResponse<?> response = HttpResponse.badRequest(); | ||
|
||
return responseProcessor.processResponse(contextBuilder | ||
.error(new DetailedError(exception.getMessage(), Map.of("sessionState", exception.getSessionState()))) | ||
.build(), response); | ||
} | ||
} |
Oops, something went wrong.