Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@nextpay/grpc-node-status-proto
Advanced tools
[![Version](https://img.shields.io/npm/v/@nextpay/grpc-node-status-proto.svg)](https://www.npmjs.com/package/@nextpay/grpc-node-status-proto) [![License](https://img.shields.io/npm/l/@nextpay/grpc-node-status-proto.svg)](https://github.com/quangtm210395/g
Readme
Utility function for serializing and deserializing between the grpc-status-details-bin
metadata value and StatusProto
when using the node grpc package. Error details allow sending/receiving additional data along with an error. For instance, if a request sends invalid data, a gRPC server could send back a BadRequest message identifying the field and why it failed validation.
gRPC services that send rich error details place information in the grpc-status-details-bin
metadata property of the ServiceError passed to the callback of a failed gRPC method call. The value of the grpc-status-details-bin
field is a serialized Status message. The Status message's details field is an array of Any messages, which consist of a type field and the serialized data for that message type.
This library, given an error, returns back the both deserialized Status message and an array of deserialized detail messages as a StatusProto object.
# yarn
yarn add @nextpay/grpc-node-status-proto
# npm
npm install @nextpay/grpc-node-status-proto
This library provide methods to serialize and deserialize grpc-status-details-bin
metadata value and StatusProto when using the node grpc package.
Version 2.x using new @grpc/grpc-js
export class StatusProto<T extends Message> {
private status: Status | undefined;
private code: number;
private message: string;
private details: Array<T>;
}
where:
status
is a google defined Statuscode
is gRPC status Codemessage
is error messagedetails
is the Status
's details array with each item deserialized and unpacked from an Any
message to its actual message type. You can create your own message type or checkout google's defined message typesStatusProto.fromServiceError
allows passing in the deserializeMap
argument, where each key is a message type and each value is its corresponding deserialize function.
You can use provided googleDeserializeMap
captured of Google's rpc error details type's deserialize function to deserialize google's Error Details
Example:
import {
googleDeserializeMap, StatusProto, BadRequest, Status,
} from "@nextpay/grpc-node-status-proto";
// Make grpc call that fails and returns a Status object with
// details in the `grpc-status-details-bin` Metadata property
function hello(message: string, metadata?: any) {
return new Promise((resolve, reject) => {
const meta = new Metadata();
if (metadata) {
Object.keys(metadata).map((key: string) => {
meta.add(key, metadata[key]);
return null;
});
}
let response = {};
const call: ClientUnaryCall = stub.hello(
{ message }, meta, async (err: any, res: any): Promise<any> => {
if (err) {
const statusProto = StatusProto.fromServiceError(err, googleDeserializeMap);
const details = statusProto.getDetails();
for (let i = 0; i < details.length; i += 1) {
const detail = details[i];
if (detail instanceof BadRequest) {
console.info('badrequest: ', detail.toObject());
// TODO do something with bad request error
}
}
console.error('calling greeter.hello failed: ', err);
reject(err);
return;
}
response = res;
},
);
call.once('status', (status: StatusObject) => {
resolve({ data: response, meta: status.metadata.getMap() });
});
});
}
statusProto.toServiceError()
convert a StatusProto to grpc 's ServiceError
Example:
import {
StatusProto, BadRequest, googleErrorDetailsTypeNameMap,
} from "@nextpay/grpc-node-status-proto";
// import others thing from grpc...
// adding hello rpc function
server.addService(Greeter.Greeter.service, {
hello(call: ServerUnaryCall<any, any>, callback: any) {
console.info('greeting: ', call.request.message);
if (!call.request.message) {
const metadata: Metadata = new Metadata();
metadata.add('1', '123');
const br = new BadRequest();
const fv = new BadRequest.FieldViolation();
fv.setField('message');
fv.setDescription('message is missing');
br.addFieldViolations(fv);
const statusProto = new StatusProto(status.INVALID_ARGUMENT, 'Required fields must not be null');
statusProto.addDetail(br, googleErrorDetailsTypeNameMap.BadRequest);
const error = statusProto.toServiceError();
return callback(error);
}
return callback(null, { message: `hi there from greeter, response for: ${call.request.message}` });
},
});
For full example, checkout here
FAQs
[![Version](https://img.shields.io/npm/v/@nextpay/grpc-node-status-proto.svg)](https://www.npmjs.com/package/@nextpay/grpc-node-status-proto) [![License](https://img.shields.io/npm/l/@nextpay/grpc-node-status-proto.svg)](https://github.com/quangtm210395/g
The npm package @nextpay/grpc-node-status-proto receives a total of 536 weekly downloads. As such, @nextpay/grpc-node-status-proto popularity was classified as not popular.
We found that @nextpay/grpc-node-status-proto demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.