Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

4.2K
Vistas
How to handle mutation GraphQL error instead of Apollo onError()

Server side, I am throwing a 403 Forbidden, to manage access to an API call.

To catch the GraphQL error for a mutation, I've tried the following: (method #1 is working when catching errors for useQuery())

const [m, { error }] = useMutation(MY_MUTATION);

if(error) {
    console.error('Error return #1');
}

try {
  await m({
    variables,
    onError: (e: ApolloError) => {
       console.error('Error return #2');
    }
  }).catch(e) {
     console.error('Error return #3');
  };
} catch (e) {
    console.error('Error return #4');
}

Instead, from my Apollo onError(), the following GraphQL errors are returned and the execution stops. It does not go into any of my mutation error handling attempts:

graphQLErrors:, Array [
  Object {
    "extensions": Object {
      "code": "FORBIDDEN",
      "response": Object {
        "error": "Forbidden",
        "message": "Access is denied.",
        "statusCode": 403,
      },
    },
    "message": "Access is denied.",
  },
]

If a similar GraphQL error is returned from useQuery(), it shows it using:

const { data, error, loading } = useQuery( MY_QUERY );

if(error) {
    console.error('Error return #1');
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Based on useMutation documentation I believe mutate (or m) function doesn't accept onError callback and you have to set it in useMutation options.

const [m, { error }] = useMutation(MY_MUTATION, {
  onError: (e: ApolloError) => {
     console.error('Error return #2');
  }
});

if (error) {
    console.error('Error return #1');
}

try {
  await m({
    variables
  })
} catch (e) {
    console.error('Error return #4');
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda