Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Better error handling?#2237

Unanswered
nboisnea asked this question inGeneral
Apr 15, 2024· 0 comments
Discussion options

I'm not sure what I am doing wrong with my error handling using apollo-angular. I have a GraphQL API listenning at localhost:8080 and I make a request that is supposed to return a GraphQL error. The server responds with a HTTP 500 error and the response's body looks like this:

{"errors": [        {"detailMessage":"Test error","statusCode":480,"message":"Error details","errorCode":1713213528628        }    ],"data": {"requestThatShouldFail":null    }}

In my Angular app I useonError with a callback function but I only get a network error, no GraphQL errors:

functionapolloLinkFactory(httpLink:HttpLink){consterrorLink=onError(({ graphQLErrors, networkError})=>{// Handle errors, but graphQLErrors is always undefined// networkError is an HttpErrorReponse});constlink=httpLink.create({uri:'http://localhost:8080'});return{cache:newInMemoryCache(),link:errorLink.concat(link)});}@NgModule({providers:[{provide:APOLLO_OPTIONS,useFactory:apolloLinkFactory,deps:[HttpLink]}]})exportclassApolloConfigModule{}

I used my browser's dev tools and it appears that the HttpErrorResponse returned by Angular's HttpClient's does not match what Appolo Client would expect when parsing the error and calling the error handler. Here is the part of Apollo Client's code that intercepts the error and calls my error handler (you can find it at @apollo/client/link/error/index.js:11):

sub=forward(operation).subscribe({next:function(result){// (...)},error:function(networkError){retriedResult=errorHandler({operation:operation,networkError:networkError,//Network errors can return GraphQL errors on for example a 403graphQLErrors:networkError&&// <-- Here my network error should be parsed to extract GraphQL errorsnetworkError.result&&networkError.result.errors,forward:forward,});if(retriedResult){retriedSub=retriedResult.subscribe({next:observer.next.bind(observer),error:observer.error.bind(observer),complete:observer.complete.bind(observer),});return;}observer.error(networkError);},complete:function(){// (...)},});

The problem is the HttpErrorResponse that is emitted does not have theresult field, causinggraphQLErrors to be left undefined.

It worked when I tried usingcreateHttpLink from Apollo Client instead of apollo-angular'sHttpLink.create(), but then it doesn't use an Angular HttpClient which I need because I use interceptors.
Am I doing something wrong? Or should apollo-angular's HttpLinkHandler pre-parse the HttpErrorResponse so it looks more like what ApolloClient expects, instead of sending it as-is?
Thanks!

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
1 participant
@nboisnea

[8]ページ先頭

©2009-2025 Movatter.jp