googleapis / googleapis/google-cloud-node

error json format assumes that object should includes 'error' fields

Open
#7,761 1 comment 0 reactions 0 assignees View on GitHub
library: google-cloud-node-core priority: p3 type: feature request
Dominant language
TypeScript
Stars
3.2k
Forks
712
Avg merge
2d 3h
Merged PRs (30d)
99

Description

## Overview

HI. I'm developing grpc-based web service with protobuf-generated SDKs.
- Spring Backend ([gapic-generator-kotlin](https://github.com/googleapis/gapic-generator-kotlin))
- Typescript Frontend ([gapic-generator-typescript](https://github.com/googleapis/gapic-generator-typescript))

when handling Error Message According to AIP Spec(mentioned below), Server gives the Error in json format.
(which satisfies AIP Error Format. I think)
``` json
{
"grpc-code":"SOME CODE",
"message":"some message"
}
```

But, When Client tries to handle this error, this error shows up.
![image](https://user-images.githubusercontent.com/30401400/180937343-21078e80-95e5-46b8-9d5e-8ce9bd0b83be.png)

According to source code (src/googleError.ts), the input `json` parameter assumes that it "should" have "error" field (which AIP Spec doesn't describe.)

I found that this format exists because Google's API Platforms compatibility reasons. but in order to support protobuf spec, Can you support protobuf Error Spec? (Google AIP Error Spec)
> While proto3 messages have native JSON encoding, Google's API Platform uses a different error schema for Google's JSON HTTP APIs for backward compatibility reasons.

https://cloud.google.com/apis/design/errors#http_mapping

## Google AIP Error Spec

- [AIP 193. Errors](https://google.aip.dev/193)
- [rpc.status.proto](https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto)

``` proto
// The `Status` type defines a logical error model that is suitable for
// different programming environments, including REST APIs and RPC APIs. It is
// used by [gRPC](https://github.com/grpc). Each `Status` message contains
// three pieces of data: error code, error message, and error details.
//
// You can find out more about this error model and how to work with it in the
// [API Design Guide](https://cloud.google.com/apis/design/errors).
message Status {
// The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code].
int32 code = 1;

// A developer-facing error message, which should be in English. Any
// user-facing error message should be localized and sent in the
// [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client.
string message = 2;

// A list of messages that carry the error details. There is a common set of
// message types for APIs to use.
repeated google.protobuf.Any details = 3;
}
```

## Current Implementation (gax-nodejs)

``` typescript
static parseHttpError(json: any): GoogleError {
if (Array.isArray(json)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json = json.find((obj: any) => {
return 'error' in obj;
});
}
const decoder = new GoogleErrorDecoder();
const proto3Error = decoder.decodeHTTPError(json['error']);
const error = Object.assign(
new GoogleError(json['error']['message']),
proto3Error
);
// Map Http Status Code to gRPC Status Code
if (json['error']['code']) {
error.code = rpcCodeFromHttpStatusCode(json['error']['code']);
} else {
// If error code is absent, proto3 message default value is 0. We should
// keep error code as undefined.
delete error.code;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.