Skip to content

Conversation

@kevcenteno
Copy link
Contributor

No description provided.

@kevcenteno kevcenteno force-pushed the kevin/fetch branch 2 times, most recently from 93ef36a to e8b57f9 Compare November 18, 2024 22:22
oalders-mm
oalders-mm previously approved these changes Nov 19, 2024
Copy link
Contributor

@oalders-mm oalders-mm left a comment

Choose a reason for hiding this comment

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

@oschwald
Copy link
Member

oschwald commented Dec 2, 2024

Should this be merged?

@kevcenteno
Copy link
Contributor Author

Ah, we missed this. It should be merged

oschwald
oschwald previously approved these changes Dec 2, 2024
Copy link
Member

@oschwald oschwald left a comment

Choose a reason for hiding this comment

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

I am approving as I assume this is the same as it was for the first review, but I did have some questions/comments.

modelClass?: any
): Promise<T>;
private responseFor(
private async responseFor(
Copy link
Member

Choose a reason for hiding this comment

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

Should async be added to the method signature on line 62 too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't seem to be necessary.

const response = await fetch(url, options);

if (!response.ok) {
return Promise.reject(await this.handleError(response, url));
Copy link
Member

Choose a reason for hiding this comment

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

I was a bit confused by this. I would have expected this to throw the exception directly (or even for handleError to do that.

I assume this is like this because you wanted to use the same catch for both the await fetch and the data = await response.json(). It would have been clearer to me to throw here and just rethrow instances of WebServiceClientError in the catch, but maybe the reasoning here is clear enough to most readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like how it separates handling "bad" server responses from client-side errors and aborts. I created a separate branch with the suggested change; let me know if it's what you had in mind: 65cd8fe

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I guess that is what I had in mind, but I agree that it ends up being a bit messier than I would like. A comment or something might help people understand why it is like this.

Potentially, you could also move the response.ok check out of the try/catch too and pass the data into handleError, but that may be a bit more involved, especially for errors where the body isn't JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

if (response.status === 204) {
return Promise.resolve();
Copy link
Member

Choose a reason for hiding this comment

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

Is this different from just a bare return?

Suggested change
return Promise.resolve();
return;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion!

const error = err as TypeError;
switch (error.name) {
case 'AbortError':
return Promise.reject({
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't you just throw these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💯

@oschwald oschwald merged commit 0e09eb5 into main Dec 3, 2024
9 checks passed
@oschwald oschwald deleted the kevin/fetch branch December 3, 2024 15:57
@oschwald
Copy link
Member

oschwald commented Dec 3, 2024

I approved and merged it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants