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

feat: update ErrorDetails to allow unpacking arbitrary messages #3073

Merged
merged 1 commit into from
Jul 29, 2024
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 @@ -126,6 +126,15 @@ public LocalizedMessage getLocalizedMessage() {
return unpack(LocalizedMessage.class);
}

/**
* Attempt to unpack a non-default error message type {@code T}. The first occurrence of a {@code
* T} is returned, null otherwise.
*/
@Nullable
public <T extends Message> T getMessage(Class<T> messageClass) {
return unpack(messageClass);
}

/** This is a list of raw/unparsed error messages that returns from server. */
@Nullable
abstract List<Any> getRawErrorMessages();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class ErrorDetailsTest {
private static final LocalizedMessage LOCALIZED_MESSAGE =
LocalizedMessage.newBuilder().setLocale("en").setMessage("nothing").build();

private static final Duration DURATION_MESSAGE =
Duration.newBuilder().setSeconds(12345).setNanos(54321).build();

ErrorDetails errorDetails;

@BeforeEach
Expand All @@ -136,7 +139,8 @@ void setUp() throws Exception {
Any.pack(REQUEST_INFO),
Any.pack(RESOURCE_INFO),
Any.pack(HELP),
Any.pack(LOCALIZED_MESSAGE));
Any.pack(LOCALIZED_MESSAGE),
Any.pack(DURATION_MESSAGE));

errorDetails = ErrorDetails.builder().setRawErrorMessages(rawErrorMessages).build();
}
Expand Down Expand Up @@ -228,4 +232,9 @@ void help_shouldUnpackHelpProtoMessage() {
void localizedMessage_shouldUnpackLocalizedMessageProtoMessage() {
Truth.assertThat(errorDetails.getHelp()).isEqualTo(HELP);
}

@Test
void getMessage_duration_shouldUnpackDurationProtoMessage() {
Truth.assertThat(errorDetails.getMessage(Duration.class)).isEqualTo(DURATION_MESSAGE);
}
}
Loading