-
Hi there! I'm using connect-web client to handle GRPC-web errors containing ErrorDetails. However, I am not sure how to use the
Which If I access A sample code to demonstrate it will do. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @boan-anbo, with the following version: v1
plugins:
- plugin: buf.build/bufbuild/es
opt: target=ts
out: gen Run the following command to generate the googleapis module, which includes error_details.proto: npx buf generate buf.build/googleapis/googleapis The new file Here is a function that finds a import {ConnectError, connectErrorDetails} from "@bufbuild/connect-web";
import {LocalizedMessage} from "./gen/google/rpc/error_details_pb.js";
function handle(err: ConnectError) {
const localizedMessages = connectErrorDetails(err, LocalizedMessage);
const currentLocaleMessage = localizedMessages.find(i => i.locale === navigator.language);
console.log(currentLocaleMessage?.message);
} You can see that Any protobuf message can be used in error details, but server and client should have some common understanding of the actual information. error_details.proto just defines a useful set of messages, but it is a good idea to start with them, and only use other types when you really need something that is missing. You can find an example for providing error details on the server side with TypeScript here. |
Beta Was this translation helpful? Give feedback.
Hey @boan-anbo,
with the following
buf.gen.yaml
:Run the following command to generate the googleapis module, which includes error_details.proto:
The new file
gen/google/rpc/error_details_pb.ts
will contain all proto messages for error details. One of them is google.rpc.LocalizedMessage, for error messages that are safe to return to the user.Here is a function that finds a
LocalizedMessage
for the current locale from the error details: