Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akihisa-shimada/91308e79985614b7ff6cbf529b2aa2ac to your computer and use it in GitHub Desktop.
Save akihisa-shimada/91308e79985614b7ff6cbf529b2aa2ac to your computer and use it in GitHub Desktop.
package com.example.proto.grpc.exception.message;
import com.example.proto.grpc.exception.message.DetailMessage;
import com.example.proto.grpc.exception.message.MessageResolver;
import com.google.protobuf.Message;
import io.grpc.Status;
import java.util.Collections;
import java.util.Set;
import static java.util.stream.Collectors.toSet;
/**
* 特定のgRPCレスポンスのための例外クラスです。
*
* @see org.springframework.web.server.ResponseStatusException
*/
public class ResponseStatusException extends RuntimeException {
final Status status;
final Set<DetailMessage> detailMessages;
public ResponseStatusException(Status status, String message) {
super(message);
this.status = status;
this.detailMessages = Collections.emptySet();
}
public ResponseStatusException(Status status, String message, Throwable cause) {
super(message, cause);
this.status = status;
this.detailMessages = Collections.emptySet();
}
public ResponseStatusException(Status status, String message, Set<DetailMessage> detailMessages) {
super(message);
this.status = status;
this.detailMessages = detailMessages;
}
public Status getStatus() {
return status;
}
public Set<? extends Message> getDetails(MessageResolver messageResolver) {
return detailMessages.stream()
.map(detail -> {
if (detail instanceof DetailMessage.TemplateMessage) {
DetailMessage.TemplateMessage templateMessage = (DetailMessage.TemplateMessage) detail;
return templateMessage.getMessage(messageResolver);
} else {
return detail.getMessage();
}
}).collect(toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment