Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
fix: empty data info is not expected
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Sep 27, 2021
1 parent 9b9395d commit 67c4da3
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/grpcServer/handlers/createGrpcErrorFromDriveResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,59 @@ function createGrpcErrorFromDriveResponse(code, info) {

const decodedInfo = info ? cbor.decode(Buffer.from(info, 'base64')) : { };

// eslint-disable-next-line prefer-destructuring
const message = decodedInfo.message;
const data = decodedInfo.data || {};

// gRPC error codes
if (code <= 16) {
const CommonErrorClass = COMMON_ERROR_CLASSES[code.toString()];
if (CommonErrorClass) {
return new CommonErrorClass(
decodedInfo.message,
createRawMetadata(decodedInfo.data),
message,
createRawMetadata(data),
);
}

// Restore stack for internal error
if (code === GrpcErrorCodes.INTERNAL) {
const error = new Error(decodedInfo.message);
error.stack = decodedInfo.data.stack;
const error = new Error(message);

// in case of verbose internal error
if (data.stack) {
error.stack = data.stack;

delete decodedInfo.data.stack;
delete data.stack;
}

return new InternalGrpcError(error, createRawMetadata(decodedInfo.data));
return new InternalGrpcError(error, createRawMetadata(data));
}

return new GrpcError(
code,
decodedInfo.message,
createRawMetadata(decodedInfo.data),
message,
createRawMetadata(data),
);
}

// Undefined Drive and DAPI errors
if (code >= 17 && code < 1000) {
return new GrpcError(
GrpcErrorCodes.UNKNOWN,
decodedInfo.message,
createRawMetadata(decodedInfo.data),
message,
createRawMetadata(data),
);
}

// DPP errors
if (code >= 1000 && code < 5000) {
const consensusError = createConsensusError(code, decodedInfo.data.arguments || []);
const consensusError = createConsensusError(code, data.arguments || []);

// Basic
if (code >= 1000 && code < 2000) {
return new InvalidArgumentGrpcError(
consensusError.message,
{ code, ...createRawMetadata(decodedInfo.data) },
{ code, ...createRawMetadata(data) },
);
}

Expand All @@ -108,23 +116,23 @@ function createGrpcErrorFromDriveResponse(code, info) {
return new GrpcError(
GrpcErrorCodes.UNAUTHENTICATED,
consensusError.message,
{ code, ...createRawMetadata(decodedInfo.data) },
{ code, ...createRawMetadata(data) },
);
}

// Fee
if (code >= 3000 && code < 4000) {
return new FailedPreconditionGrpcError(
consensusError.message,
{ code, ...createRawMetadata(decodedInfo.data) },
{ code, ...createRawMetadata(data) },
);
}

// State
if (code >= 4000 && code < 5000) {
return new InvalidArgumentGrpcError(
consensusError.message,
{ code, ...createRawMetadata(decodedInfo.data) },
{ code, ...createRawMetadata(data) },
);
}
}
Expand Down

0 comments on commit 67c4da3

Please sign in to comment.