menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Studio
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

404 error handling with apollo-link-rest

January 2, 2020 at 10:19am

404 error handling with apollo-link-rest

January 2, 2020 at 10:19am (Edited 7 months ago)
I'm using apollo-link-rest for calling REST endpoint. But it throws null when endpoint returns 404 error instead of error. Can anyone help me please? Here's my apollo client setup code
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { RestLink } from 'apollo-link-rest'
import { onError } from 'apollo-link-error'
import { ApolloLink } from 'apollo-link'
const restLink = new RestLink({
uri:'example.com'
})
const authRestLink = new ApolloLink((operation, forward) => {
operation.setContext(({ headers }) => {
const token = localStorage.getItem('token')
const accessCode = localStorage.getItem('accessCode')
return {
headers: {
...headers,
Accept: 'application/json',
Authorization: token ? `Bearer ${token}` : null,
'Access-code': accessCode ? accessCode : null
}
}
})
return forward(operation).map(result => {
const { restResponses } = operation.getContext()
if (restResponses[0].status === 404) { <------ I can get restResponse status 404 here
// return new Observable(observer => {
// forward(operation).subscribe({
// error: restResponses => {
// if (restResponses[0].status === 404) {
// return observer.next({ errors: [restResponses[0].status] })
// }
// observer.error(restResponses[0].status)
// },
// // error: observer.next({ errors: [restResponses[0].status] }),
// complete: observer.complete.bind(observer)
// })
// })
}
const authTokenResponse = restResponses.find(res =>
res.headers.has('Authorization')
)
// You might also filter on res.url to find the response of a specific API call
if (authTokenResponse) {
localStorage.setItem(
'token',
authTokenResponse.headers.get('Authorization')
)
}
return result
})
})
const linkError = onError(({ networkError, forward, operation }) => {
if (networkError) {
if (networkError.result && networkError.result.code === 'INVALID\_TOKEN') {
localStorage.setItem('isAuthenticated', false)
window.location.href = `${window.location.protocol}//${ window.location.hostname }${window.location.port ? ':' + window.location.port : ''}/register`
return forward(operation)
}
}
})
const link = ApolloLink.from([authRestLink, linkError, restLink])
const client = new ApolloClient({
link: link,
cache: new InMemoryCache()
})
export default client
when I'm calling query from component and API endpoint returns 404 then it returns response null.
Can anyone suggest me any recommended way to how to handle 404 error in apollo-link-rest
client
.query({
query: Login_CHECK,
variables: {
data
}
})
.then(res => {
console.log(res) <-----It goes in response block
}).catch((err) => {console.log(err)// undefined}) . <-----It should go in catch block

July 2, 2020 at 2:26am
Did you ever find a solution to the issue - I am having the same problem
  • reply
  • like

July 15, 2020 at 3:51pm
can you please format your code so it is at least readable
  • reply
  • like