Skip to content
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

[Feat]: Throwing REST errors with request object #3368

Open
DarkMio opened this issue Jan 12, 2024 · 3 comments
Open

[Feat]: Throwing REST errors with request object #3368

DarkMio opened this issue Jan 12, 2024 · 3 comments
Labels
no-issue-activity t-feat A new feature w-verified This had been verified

Comments

@DarkMio
Copy link

DarkMio commented Jan 12, 2024

Tell us what problem the feature would solve

Currently the thrown errors lack the context in which they are thrown and requires to parse the error message to figure out what went wrong. Especially with expected errors (404, for example), this causes friction.

Describe the solution you'd like

Axios has a tiny helper that smoothes over some edges: https://github.com/axios/axios/blob/6d4c421ee157d93b47f3f9082a7044b1da221461/lib/helpers/isAxiosError.js#L12

with that it's possible to write code such as:

try {
  return await axios.get<Result>(uri);
} catch (error) {
  // 404 is to be expected
  if(isAxiosError(error) && error.request.status == 404) {
    return;
  }
  throw error;
}

Describe alternatives you've considered

There are none beyond parsing the Error text.

Additional Info

The code here: https://github.com/discordeno/discordeno/blob/main/packages/rest/src/manager.ts#L477-L482

could be replace with:

export class DiscordRestErro extends Error {
  constructor(message?: string, request: Awaited<ReturnType<typeof fetch>>) { ... }
  isDiscordRestError: true
  ...
}
// ... 

if (!result.ok) {
  const err = (await result.json().catch(() => {})) as Record<string, any>
  // Legacy Handling to not break old code or when body is missing
  if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`)
  
  throw new DiscordRestError(JSON.stringify(err), result);
}
@DarkMio DarkMio added the t-feat A new feature label Jan 12, 2024
@github-actions github-actions bot added the w-unverified This has not been verified label Jan 12, 2024
@Fleny113
Copy link
Contributor

I think you are referring to the normal rest errors, that get throw at L358, L382 and L391, the problem is that even if you replace the current object rejection (that creates an error since they are unhandled) the stack trace would be lost and the only information is that comes from Discordeno. I'm not at the moment very much familiar with how the Promise constructor deals with stack traces as we do use it a few times

note: the code you put in the additional note is for the Rest proxy (when you send the rest request for discord to your server to then send it to discord, this is helpful when multiple application need to share the same resources that have a shared ratelimit)

@Fleny113
Copy link
Contributor

With #3382 the error will get a stack trace but i didn't mark the pr to close this issue as I'm not convinced that adding the entire request object returned by fetch is a good idea

You still get the body (unless it is an error caused by too many retries for ratelimit / a fetch error, then there is no body but an error) and the status code from the request in the error.cause object

This comment was marked as outdated.

@AwesomeStickz AwesomeStickz added w-verified This had been verified and removed w-unverified This has not been verified labels Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-issue-activity t-feat A new feature w-verified This had been verified
Projects
None yet
Development

No branches or pull requests

3 participants