-
Notifications
You must be signed in to change notification settings - Fork 16
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
improve shell error handling #330
Changes from 1 commit
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,27 +1,18 @@ | ||||||||||
import { connect, constants } from "http2"; | ||||||||||
|
||||||||||
// Copied from the fauna-js driver: | ||||||||||
// https://github.com/fauna/fauna-js/blob/main/src/http-client/node-http2-client.ts | ||||||||||
import fetch from "node-fetch"; | ||||||||||
|
||||||||||
export type QueryResponse<T> = QuerySuccess<T> | QueryFailure; | ||||||||||
|
||||||||||
export type QueryInfo = { | ||||||||||
headers: any; | ||||||||||
body: { | ||||||||||
summary: string; | ||||||||||
}; | ||||||||||
}; | ||||||||||
|
||||||||||
export type QuerySuccess<T> = QueryInfo & { | ||||||||||
export type QuerySuccess<T> = { | ||||||||||
status: 200; | ||||||||||
body: { | ||||||||||
data: T; | ||||||||||
}; | ||||||||||
}; | ||||||||||
|
||||||||||
export type QueryFailure = QueryInfo & { | ||||||||||
status: 400; | ||||||||||
export type QueryFailure = { | ||||||||||
status: number; | ||||||||||
body: { | ||||||||||
summary?: string; | ||||||||||
error: { | ||||||||||
code: string; | ||||||||||
message?: string; | ||||||||||
|
@@ -30,16 +21,12 @@ export type QueryFailure = QueryInfo & { | |||||||||
}; | ||||||||||
|
||||||||||
export default class FaunaClient { | ||||||||||
session: any; | ||||||||||
endpoint: string; | ||||||||||
secret: string; | ||||||||||
timeout?: number; | ||||||||||
|
||||||||||
constructor(opts: { endpoint: string; secret: string; timeout?: number }) { | ||||||||||
this.session = connect(opts.endpoint, { | ||||||||||
peerMaxConcurrentStreams: 50, | ||||||||||
}) | ||||||||||
.once("error", () => this.close()) | ||||||||||
.once("goaway", () => this.close()); | ||||||||||
this.endpoint = opts.endpoint; | ||||||||||
this.secret = opts.secret; | ||||||||||
this.timeout = opts.timeout; | ||||||||||
} | ||||||||||
|
@@ -57,57 +44,39 @@ export default class FaunaClient { | |||||||||
typecheck: opts?.typecheck ?? undefined, | ||||||||||
secret: opts?.secret ?? this.secret, | ||||||||||
}; | ||||||||||
return new Promise((resolvePromise, rejectPromise) => { | ||||||||||
let req: any; | ||||||||||
const onResponse = (http2ResponseHeaders: any) => { | ||||||||||
const status = http2ResponseHeaders[constants.HTTP2_HEADER_STATUS]; | ||||||||||
let responseData = ""; | ||||||||||
const url = new URL(this.endpoint); | ||||||||||
url.pathname = "/query/1"; | ||||||||||
const res = await fetch(url, { | ||||||||||
method: "POST", | ||||||||||
headers: { | ||||||||||
authorization: `Bearer ${secret ?? this.secret}`, | ||||||||||
"x-fauna-source": "Fauna Shell", | ||||||||||
...(typecheck !== undefined && { "x-typecheck": typecheck.toString() }), | ||||||||||
...(format !== undefined && { "x-format": format }), | ||||||||||
}, | ||||||||||
body: JSON.stringify({ query }), | ||||||||||
}); | ||||||||||
|
||||||||||
req.on("data", (chunk: any) => { | ||||||||||
responseData += chunk; | ||||||||||
}); | ||||||||||
const json = await res.json(); | ||||||||||
|
||||||||||
req.on("end", () => { | ||||||||||
resolvePromise({ | ||||||||||
status, | ||||||||||
body: JSON.parse(responseData), | ||||||||||
headers: http2ResponseHeaders, | ||||||||||
}); | ||||||||||
}); | ||||||||||
if (res.status === 200 || res.status === 201) { | ||||||||||
return { | ||||||||||
status: 200, | ||||||||||
body: { | ||||||||||
data: json.data as T, | ||||||||||
}, | ||||||||||
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 should just be
Suggested change
so that the 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. o I see, thats why the summary field was shared across error and success types 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. yeah. you can also get log messages back in the log("hi"); 1/0
// returns
divide_by_zero: Attempted integer division by zero.
error: Attempted integer division by zero.
at *query*:1:14
|
1 | log('hi'); 1/0
| ^
|
info at *query*:1: hi |
||||||||||
}; | ||||||||||
|
||||||||||
try { | ||||||||||
const httpRequestHeaders = { | ||||||||||
Authorization: `Bearer ${secret}`, | ||||||||||
"x-format": format, | ||||||||||
"X-Fauna-Source": "Fauna Shell", | ||||||||||
[constants.HTTP2_HEADER_PATH]: "/query/1", | ||||||||||
[constants.HTTP2_HEADER_METHOD]: "POST", | ||||||||||
...((typecheck && { "x-typecheck": typecheck }) ?? {}), | ||||||||||
...((this.timeout && { "x-query-timeout-ms": this.timeout }) ?? {}), | ||||||||||
}; | ||||||||||
|
||||||||||
req = this.session | ||||||||||
.request(httpRequestHeaders) | ||||||||||
.setEncoding("utf8") | ||||||||||
.on("error", (error: any) => rejectPromise(error)) | ||||||||||
.on("response", onResponse); | ||||||||||
|
||||||||||
req.write(JSON.stringify({ query }), "utf8"); | ||||||||||
|
||||||||||
// req.setTimeout must be called before req.end() | ||||||||||
req.setTimeout((this.timeout ?? 0) + 5000, () => { | ||||||||||
req.destroy(new Error(`Client timeout`)); | ||||||||||
}); | ||||||||||
|
||||||||||
req.end(); | ||||||||||
} catch (error) { | ||||||||||
rejectPromise(error); | ||||||||||
} | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
async close() { | ||||||||||
this.session.close(); | ||||||||||
} else { | ||||||||||
return { | ||||||||||
status: res.status, | ||||||||||
body: { | ||||||||||
summary: json.summary, | ||||||||||
error: { | ||||||||||
code: json.error?.code, | ||||||||||
message: json.error?.message, | ||||||||||
}, | ||||||||||
}, | ||||||||||
}; | ||||||||||
} | ||||||||||
} | ||||||||||
} |
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.
the
x-query-timeout-ms
header should be sent hereThere 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.
good call, looks like we also weren't correctly passing the timeout to the client so resolving that as well.
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.
oh cool, nice