Skip to content

Conversation

@david-crespo
Copy link
Collaborator

@david-crespo david-crespo commented Mar 10, 2022

Added to spec in oxidecomputer/dropshot#286 and integrated into Omicron in oxidecomputer/omicron#710. First commit is setup; the meat is in 7e6c428.

Originally (see intermediate commits if you dare) this was meant to be fancy and add per-endpoint error response types. However, the whole point of the way Dropshot is written is that the error type is the same on all endpoints and is not generic, which saves us a world of pain on the console side. So instead all I really did was get rid of the error type parameter entirely.

  • Rename HttpResponse to ApiResponse
  • Make ApiResponse a discriminated union of success and error
  • Get rid of second param in request<Instance, Error>
  • Change some promise stuff to async/await for consistency

Api.ts Outdated
params: RequestParams = {}
) =>
this.request<RackResultsPage, any>({
this.request<RackResultsPage, Error>({
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems simple right

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

famous last words

.filter(isTruthy)
.map(refToResponseName)
// need to dedupe because 4xx and 5xx are the same for now
.reduce(uniq, [] as string[]);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feeling smart as hell for this one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I either want like a do() function just to run something on the whole array or the pipe operator. It'd make this moot.

Copy link
Collaborator Author

@david-crespo david-crespo Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly looks like pipeline operator is not moving much. We might get pipe() sooner than that, and we can just write our own if we want that.

https://github.com/tc39/proposal-pipeline-operator/blob/main/HISTORY.md
https://github.com/js-choi/proposal-function-pipe-flow

Comment on lines 25 to 30
export function uniq<T>(acc: T[], next: T): T[] {
if (!acc.includes(next)) {
acc.push(next);
}
return acc;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might not be a big deal given it'd probably be used with small input sizes, but I believe this operates in quadratic time. For every N items in the list you're having to do N comparisons to verify if the thing is in the list or not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not worried about it yet because N = 2, plus it's only happening at codegen time, not even build time

}

if (!r.ok) throw r;
return r;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stuff doesn't change any functionality, I just thought it was weird that we were mixing promises and async/await


export type ApiResponse<Data extends unknown> =
| SuccessResponse<Data>
| ErrorResponse;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we can actually discriminate between the two

@david-crespo david-crespo merged commit 4705ceb into main Mar 14, 2022
@david-crespo david-crespo deleted the error-types branch March 14, 2022 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants