0

React is showing the regular developer errors even in the built and I want to handle at least some with errorboundray or similar, the actual customized errors are thrown by mongoose in my apollo client / express server / graphql implementation, apparently, componentDidCatch is not catching any. I have achieved to show some of them in order to be human-readable errors and a proper rendering but I can't cut the developer error behavior from React built, is it possible to implement a straight handler or add a className to the errors so they could be hidden in CSS?

Errors:

Uncaught (in promise) Error: GraphQL error:

ErrorBoundary:

import React, { Component } from "react";

export class ErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = { error: null, errorInfo: null };
  }

  componentDidCatch(error, errorInfo) {
    this.setState({
      error: error,
      errorInfo: errorInfo
    });
  }

  render() {
    if (this.state.errorInfo) {
      return (
        <div>
          <h2>Something went wrong</h2>
          <details style={{ whiteSpace: "pre-wrap" }}>
            {this.state.error && this.state.error.toString()}
            <br />
            {this.state.errorInfo.componentStack}
          </details>
        </div>
      );
    }
    // Render children if there's no error
    return this.props.children;
  }
}

export default ErrorBoundary;

Wrap:

import React from "react";
import Facebook from "./Facebook";
import Invoice from "./Invoice";
import ErrorBoundary from "./ErrorBoundary";

const Login = () => {
  return (
    <div>
      <section className="header_text sub">
        <img
          className="pageBanner"
          src="themes/images/pageBanner.png"
          alt="New products"
        />
        <h4>
          <span>Sesión y Registro</span>
        </h4>
      </section>
      <section className="main-content">
        <div className="row">
          <div className="span5">
            <Facebook />
          </div>
          <ErrorBoundary>
            <Invoice />
          </ErrorBoundary>
        </div>
      </section>
    </div>
  );
};

export default Login;

Component:

import React, { useState } from "react";
import { useSelector } from "react-redux";
import { useMutation } from "@apollo/react-hooks";
import { useDispatch } from "react-redux";
import { addClientMutation, getClientQuery } from "../queries/queries";
import { addClientAction } from "../actions";
import ReCAPTCHA from "react-google-recaptcha";

const Invoice = props => {
  let client = useSelector(state => state.login);

  const dispatch = useDispatch();

  const [newClient, setNewClient] = useState([]);

  const [formDisable, setFormDisable] = useState(true);

  const [addClientToDB, { error }] = useMutation(addClientMutation);

  function refreshPage() {
    window.location.reload(false);
  }

  if (error) {
    console.log("error:", error);
...
Lester Arévalo
  • 342
  • 1
  • 6
  • 12
  • Duplicate of [Handling errors with react-apollo useMutation hook](https://stackoverflow.com/questions/59465864/handling-errors-with-react-apollo-usemutation-hook). Make sure you are properly handling any rejected Promises returned by your `mutate` call as shown in the linked post. – Daniel Rearden Mar 30 '20 at 23:39
  • thanks!, this information led me to: const [updateResource] = useMutation(UPDATE_RESOURCE, { onCompleted: (data) => { alert(` ${data.updateResource.resourceName} Successfully Updated!`) }, onError: (error) => alert(`Resource Not Updated: ${error.message.split('GraphQL error: ')[1]}`) }) ref: https://spectrum.chat/apollo/react-apollo/handling-usemutation-errors-in-the-frontend~281c9121-48a3-48ef-a621-c9f05e4d3fcd – Lester Arévalo Mar 31 '20 at 00:44

0 Answers0