Skip to content

Conversation

@Oksamies
Copy link
Contributor

(thunderstore-api) Implement user-facing error handling improvements and update tests for authentication errors

(thunderstore-api) Refactor API fetch functions to remove async/await where unnecessary for improved performance

@coderabbitai
Copy link

coderabbitai bot commented Nov 19, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 11-19-_thunderstore-api_implement_user-facing_error_handling_improvements_and_update_tests_for_authentication_errors

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor Author

Oksamies commented Nov 19, 2025

This was referenced Nov 19, 2025
@Oksamies Oksamies marked this pull request as ready for review November 27, 2025 11:55
queryParams: {},
})
).rejects.toThrowError("401: Unauthorized");
).rejects.toThrowError("Authentication required. Please sign in.");
Copy link
Contributor

Choose a reason for hiding this comment

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

If the thrown error gives us clean access to the status code, it could be less brittle to check that instead of the message. Unless the message itself is the very behaviour we are interested in.

const MAX_NB_RETRY = 5;
const RETRY_DELAY_MS = 200;

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: I think the function name is pretty self explanatory and this comment is unnecessary.

}
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: I think the function name is pretty self explanatory and this comment is unnecessary. Also "used by X" comments are a bit problematic as they tend to get easily outdated. I guess there might be some cases where they're warranted but this doesn't seem one of those to me.

}

export type apiFetchArgs<B, QP> = {
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is actively confusing me. It seems it would be more suited for apiFetchArgs than SchemaOrUndefined. And if it would be a comment for apiFetchArgs I would nitpick that it provides no value.

type SchemaOrUndefined<Schema extends z.ZodSchema | undefined> =
Schema extends z.ZodSchema ? z.infer<Schema> : undefined;

export type apiFetchArgs<
Copy link
Contributor

Choose a reason for hiding this comment

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

I've noticed in the few last PRs that especially types seems to be exported even when they're used only locally. I would rather not export stuff just in case. It's easy to add export later if needed, removing it is more effort if it's used somewhere outside already. And I think it also increases the odds of IDEs autoimporting stuff from these files directly, instead of the root of the package where the public interface of the package should be defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This export probably a remnant of where it was used in the fetch functions. Removed the export

responseJson = undefined;
}

const serverMessage = responseJson
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm reading this correctly, extract message looks like it could handle things if we just did const serverMessage = extractMessage(responseJson ?? responseText);

message: buildApiErrorMessage({ response, responseJson, context }),
response: response,
responseJson: responseJson,
responseText,
Copy link
Contributor

Choose a reason for hiding this comment

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

This object duplicates the data. It might be reasonable if we assume the error responses from the API are short, and having both the original data and the "ready-to-use" data available can improve DX. I'm just wondering does this cause issues e.g. if the API returns e.g. the whole Django 404 page (as I think it does if the API endpoint doesn't exists). Granted, at that point the error message isn't our biggest problem.

Just thinking out loud, doesn't require action at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mm, I guess it could be handy if it's there, but I'm not sure to which extent? Definitely something that can be added if the need arises 👍

}
}

function buildApiErrorMessage(args: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sidenote: the implementation on L177-L201 looks awful.

That being said I'd repeat the comment from the previous PR: this doing UI level stuff in a package that should focus on HTTP traffic. IMO error messages should be handled in cyberstorm-remix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I see it the parts of the error message handling that should be in cyberstorm-remix are there and the stuff that shouldn't be there are in here. Mentioned this in another reply, but I'd be happy to refactor the implementation on a separate PR if we come to a consensus on the separation 👍

}

function extractMessage(value: JSONValue | undefined): string | undefined {
if (value === undefined || value === null) return undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Can value ever be null here? Or if we want to accept nulls, should the argument types be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JSONValue type can be null

}
}

for (const entry of Object.values(objectValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this have a decent chance of us showing just some random garbage to the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function has a very good chance of getting something out of the given input, but the where it's used, I'd say it's a very little risk of showing random garbage. Though en pistä päätä pantiks, it's been a long day so can't think straight anymore

@Oksamies Oksamies changed the base branch from 11-19-implement_nimbus_error_handling_components_and_utilities_for_consistent_user-facing_error_messages to graphite-base/1614 December 2, 2025 13:08
@Oksamies Oksamies force-pushed the 11-19-_thunderstore-api_implement_user-facing_error_handling_improvements_and_update_tests_for_authentication_errors branch from 635eec4 to f78b11b Compare December 2, 2025 13:09
@Oksamies Oksamies changed the base branch from graphite-base/1614 to 11-19-implement_nimbus_error_handling_components_and_utilities_for_consistent_user-facing_error_messages December 2, 2025 13:09
@Oksamies Oksamies changed the base branch from 11-19-implement_nimbus_error_handling_components_and_utilities_for_consistent_user-facing_error_messages to graphite-base/1614 December 3, 2025 01:44
@Oksamies Oksamies force-pushed the 11-19-_thunderstore-api_implement_user-facing_error_handling_improvements_and_update_tests_for_authentication_errors branch from f78b11b to 13fecd4 Compare December 3, 2025 01:45
@Oksamies Oksamies changed the base branch from graphite-base/1614 to 11-19-implement_nimbus_error_handling_components_and_utilities_for_consistent_user-facing_error_messages December 3, 2025 01:45
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.

4 participants