Skip to content

Commit

Permalink
core: handle generic exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
eckter committed Nov 29, 2023
1 parent 35c113a commit 825d32c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public enum ErrorType {
"assert check failed",
ErrorCause.INTERNAL
),
GenericExceptionError(
"generic_exception_error",
"A generic 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 for a generic throwable.
*
* @param error the throwable error
* @return a new OSRDError instance
*/
public static OSRDError newGenericExceptionError(Throwable error) {
var wrapper = new OSRDError(ErrorType.GenericExceptionError);
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.newGenericExceptionError(ex));
}
}

Expand Down

0 comments on commit 825d32c

Please sign in to comment.