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

Fix #1107: log unmapped exceptions in addition to mapping #1112

Merged
merged 1 commit into from
May 22, 2024
Merged
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 @@ -17,12 +17,16 @@
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Simple mapper for mapping {@link Throwable}s to {@link CommandResult.Error}, with a default
* implementation.
*/
public final class ThrowableToErrorMapper {
private static final Logger logger = LoggerFactory.getLogger(ThrowableToErrorMapper.class);

private static final BiFunction<Throwable, String, CommandResult.Error> MAPPER_WITH_MESSAGE =
(throwable, message) -> {
// if our own exception, shortcut
Expand All @@ -46,6 +50,10 @@ public final class ThrowableToErrorMapper {
}

// handle all other exceptions
logger.error(
String.format(
"Unrecognized exception caught, mapped to SERVER_UNHANDLED_ERROR: %s", message),
throwable);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will log, including stack trace, when Throwable passed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible for this to be tied to tenant ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to see: cannot pass directly (one of things I dislike about Dependency Injection style), but perhaps there's a way with context (MDC?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we do set tenantId entry of MDC in MeteredCommandProcessor. And assuming the new logger uses default formatting, and that is includes contents of MDC under "mdc" key (which I think) happens, I think it should be automatically included.

return ErrorCode.SERVER_UNHANDLED_ERROR
.toApiException("root cause: (%s) %s", throwable.getClass().getName(), message)
.getCommandResultError(Response.Status.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -166,6 +174,10 @@ private static CommandResult.Error handleAllNodesFailedException(
}
}
// should not happen
logger.error(
String.format(
"Unrecognized exception caught, mapped to SERVER_UNHANDLED_ERROR: %s", message),
throwable);
return ErrorCode.SERVER_UNHANDLED_ERROR
.toApiException("root cause: (%s) %s", throwable.getClass().getName(), message)
.getCommandResultError(Response.Status.INTERNAL_SERVER_ERROR);
Expand Down