-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance HTTP Status handling #1472
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,9 +136,8 @@ class GrpcWebClientReadableStream { | |
|
||
const self = this; | ||
events.listen(this.xhr_, EventType.READY_STATE_CHANGE, function(e) { | ||
let contentType = self.xhr_.getStreamingResponseHeader('Content-Type'); | ||
if (!contentType) return; | ||
contentType = contentType.toLowerCase(); | ||
const contentType = (self.xhr_.getStreamingResponseHeader('Content-Type') || '').toLowerCase(); | ||
if (!contentType.startsWith('application/grpc')) return; | ||
|
||
let byteSource; | ||
if (googString.startsWith(contentType, 'application/grpc-web-text')) { | ||
|
@@ -152,11 +151,8 @@ class GrpcWebClientReadableStream { | |
} else if (googString.startsWith(contentType, 'application/grpc')) { | ||
byteSource = new Uint8Array( | ||
/** @type {!ArrayBuffer} */ (self.xhr_.getResponse())); | ||
} else { | ||
self.handleError_( | ||
new RpcError(StatusCode.UNKNOWN, 'Unknown Content-type received.')); | ||
return; | ||
} | ||
|
||
let messages = null; | ||
try { | ||
messages = self.parser_.parse(byteSource); | ||
|
@@ -257,11 +253,14 @@ class GrpcWebClientReadableStream { | |
return; | ||
} | ||
let errorMessage = ErrorCode.getDebugMessage(lastErrorCode); | ||
|
||
const errorMetadata = /** @type {!StatusMetadata} */ ({}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line won't pass closure compilation (it will break internal tests, but not sure why it's not throwing up in tests :)) — since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How then can we define it as a type for Closure JS? How is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this about creating file like this: javascript/net/grpc/web/metadata.js ? |
||
if (xhrStatusCode != -1) { | ||
errorMessage += ', http status code: ' + xhrStatusCode; | ||
errorMetadata['httpStatusCode'] = xhrStatusCode; | ||
} | ||
|
||
self.handleError_(new RpcError(grpcStatusCode, errorMessage)); | ||
self.handleError_(new RpcError(grpcStatusCode, errorMessage, errorMetadata)); | ||
return; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
declare module "grpc-web" { | ||
|
||
export interface Metadata { [s: string]: string; } | ||
export type StatusMetadata = Metadata & { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I appreciate the type extension, and i personally prefer this kind of change. However, since I plan to have this feature only in open source grpc-web, i prefer to make minimal changes to the rest of the API (e.g. in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my first tought, but, Metadata type is used both for input and output to grpc-services. Also i assume that previous definition od Metadata follows this: https://grpc.io/docs/guides/metadata/#overview (keys are string, values are string or binary). To not make a breaking change for those who rely on I think this is more secure way to handle Metadata for status and error. Don't see any reason why That's why i decided to make separate type. |
||
httpStatusCode?: number | ||
}; | ||
|
||
export class AbstractClientBase { | ||
thenableCall<REQ, RESP> ( | ||
|
@@ -105,15 +108,15 @@ declare module "grpc-web" { | |
} | ||
|
||
export class RpcError extends Error { | ||
constructor(code: StatusCode, message: string, metadata: Metadata); | ||
constructor(code: StatusCode, message: string, metadata: StatusMetadata); | ||
code: StatusCode; | ||
metadata: Metadata; | ||
metadata: StatusMetadata; | ||
} | ||
|
||
export interface Status { | ||
code: number; | ||
details: string; | ||
metadata?: Metadata; | ||
metadata?: StatusMetadata; | ||
} | ||
|
||
export enum StatusCode { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain a bit more on the motivation of all these changes (besides just adding the
httpStatusCode
in metadata)?It looks to me that it's altering some existing logic, and I'm not sure if it can cause regressions.
From the PR description, I got the impression that adding
httpStatusCode
was all we needed.Is that not the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, already described it above: #1472 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sampajano can we move this forward?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wszydlak Hey sorry i was caught up with other business.. I'll try to get back to this early next week!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sampajano any updates?