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

core: handle generic exceptions #5916

Merged
merged 1 commit into from
Nov 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public enum ErrorType {
"assert check failed",
ErrorCause.INTERNAL
),
UnknownError(
"unknown_error",
"An unknown exception was thrown",
ErrorCause.INTERNAL
),
InfraSoftLoadingError(
"infra_loading:soft_error",
"soft error while loading new infra",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ public static OSRDError newAssertionWrapper(AssertionError assertionError) {
return error;
}

/**
* Creates a new OSRDError from an unknown Throwable.
*
* @param error the throwable error
* @return a new OSRDError instance
*/
public static OSRDError newUnknownError(Throwable error) {
var wrapper = new OSRDError(ErrorType.UnknownError);
wrapper.context.put("message", error.getMessage());
wrapper.context.put("exception_type", error.getClass().toString());
wrapper.context.put("stack_trace", convertStackTrace(error.getStackTrace()));
return wrapper;
}

/**
* Creates a new OSRDError for an infrastructure loading error.
*
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/fr/sncf/osrd/api/ExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public static Response handle(Throwable ex) {
else if (ex instanceof AssertionError)
return toResponse(OSRDError.newAssertionWrapper((AssertionError) ex));
else {
return new RsWithStatus(
new RsWithBody(ex.toString()),
500
);
return toResponse(OSRDError.newUnknownError(ex));
}
}

Expand Down