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

isErrorFromAlias not finding errors when uri have undefined params #402

Closed
Zehir opened this issue Mar 30, 2023 · 3 comments
Closed

isErrorFromAlias not finding errors when uri have undefined params #402

Zehir opened this issue Mar 30, 2023 · 3 comments

Comments

@Zehir
Copy link
Contributor

Zehir commented Mar 30, 2023

Hello,

TLDR; isErrorFromAlias is checking the isErrorFromPath does.

I have an issue where, now I can fix easily but what really hard to debug.

I was using isErrorFromAlias but was returning false and I was not able to figure out why. I tried using a z.any() schema, an 'default' status but none of them worked.

After copying the code of isErrorFromAlias in my project and add many many console.log I was able to find the issue.

It's related to this code :

zodios/src/utils.ts

Lines 111 to 125 in e8232fc

export function findEndpointErrorsByAlias(
api: ZodiosEndpointDefinitions,
alias: string,
err: AxiosError
) {
const endpoint = findEndpointByAlias(api, alias);
return endpoint &&
err.config &&
err.config.url &&
endpoint.method === err.config.method &&
pathMatchesUrl(endpoint.path, err.config.url)
? findEndpointErrors(endpoint, err)
: undefined;
}

Even it's looking for the endpoint using the alias it's still checking the uri.

The issue is in the pathMatchesUrl method ;

zodios/src/utils.ts

Lines 127 to 130 in e8232fc

export function pathMatchesUrl(path: string, url: string) {
return new RegExp(`^${path.replace(paramsRegExp, () => "([^/]+)")}$`).test(
url
);

But in my case the endpoint URI is /api/:apiKey/config/whitelist/:oldApiKey and the url of the Axios response is /api/SOME_KEY/config/whitelist/

The regex is resolved to /api/([^/]+)/config/whitelist/([^/]+) but that don't match the response url because oldApiKey was undefined.

The easy fix it to make sure the oldApiKey is defined.

My question, if it's looking with the alias why it's still checking the uri ?

And a side question, does the alias need to be uniques in the api ?

Thanks

@ecyrbe
Copy link
Owner

ecyrbe commented Mar 31, 2023

it's checking the uri, because that's all the error that got rejected has to provide to match the call. The alias alone can't allow to match the error uri.

In v11, i'am now evaluating to remove this entirely and make the call return the object error, like this :

const { data: user, error } = client.getUser();

instead of :

try {
 const user = client.getUser();
 } catch (e) {
   if(isErrorFromAlias('getUser')) {
   ...
   }
 }

But the decision to do this breaking change is not yet settled

@Zehir
Copy link
Contributor Author

Zehir commented Apr 1, 2023

Yes but you could use the alias to get the endpoint (like you did) and only the status for the error ;

   const endpoint = findEndpointByAlias(api, alias); 
  
   return (endpoint && 
     err.config && 
     endpoint.method === err.config.method)
     ? findEndpointErrors(endpoint, err) 
     : undefined; 

Also you probably should add ( ) around the condition, it's more readable and typescript don't complain that way

@thelinuxlich
Copy link
Contributor

thelinuxlich commented Apr 6, 2023

I'm using https://github.com/causaly/zod-validation-error for better errors:

if(err instanceof ZodiosError) {
   if (err.cause instanceof ZodError) {
      console.log(fromZodError(err.cause).toString())
   }
}

@Zehir Zehir closed this as completed Apr 9, 2023
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

No branches or pull requests

3 participants