HackYourFuture-CPH / simply-name.it

Final Project for Class17
MIT License
3 stars 1 forks source link

ApiError added #239

Closed SlaviaF closed 3 years ago

SlaviaF commented 3 years ago

Description

class ApiError added so as to handle error from fetching api

Fixes #237

How to test?

Please provide a short summary how your changes can be tested? if there are errors while fetching it will console the api error

Checklist

senner007 commented 3 years ago

+1 for the message, but we need the rendering in the error boundary not the error class.

oops sorry. Classes give me a little bit of a spin in the head

You'll have to spin in the other direction in your chair to equalize it then :)

SlaviaF commented 3 years ago

+1 for the message, but we need the rendering in the error boundary not the error class. oops sorry. Classes give me a little bit of a spin in the head

You'll have to spin in the other direction in your chair to equalize it then :)

I tried a lot of things to return another message in the render function(in case if its an api error), but it doesn't work. Or do I have to alter the existing one.

senner007 commented 3 years ago

+1 for the message, but we need the rendering in the error boundary not the error class. oops sorry. Classes give me a little bit of a spin in the head

You'll have to spin in the other direction in your chair to equalize it then :)

I tried a lot of things to return another message in the render function(in case if its an api error), but it doesn't work. Or do I have to alter the existing one.

How about this:

export class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, errorType: null};
  }

  static getDerivedStateFromError(error) {
    console.log(error instanceof ApiError);
    return { hasError: true, errorType: error };
  }

  render() {
    if (this.state.hasError) {
      if (this.state.errorType instanceof ApiError) {
         if (this.state.errorType.statusCode === 404) {
            // return 404 component
         }
          if (this.state.errorType.statusCode === 403) {
            // return 403 component
         }
         if (this.state.errorType.statusCode === 400) {
            // return 400 component
         }
          if (this.state.errorType.statusCode === 500) {
            // return 500 component
         }
         // return generic message here
      }
        return <h1>Uh-oh! Cannot render app, check console.</h1>;
    }

    return this.props.children;
  }
}
SlaviaF commented 3 years ago

+1 for the message, but we need the rendering in the error boundary not the error class. oops sorry. Classes give me a little bit of a spin in the head

You'll have to spin in the other direction in your chair to equalize it then :)

I tried a lot of things to return another message in the render function(in case if its an api error), but it doesn't work. Or do I have to alter the existing one.

How about this:

export class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false, errorType: null};
  }

  static getDerivedStateFromError(error) {
    console.log(error instanceof ApiError);
    return { hasError: true, errorType: error };
  }

  render() {
    if (this.state.hasError) {
      if (this.state.errorType instanceof ApiError) {
         if (this.state.errorType.statusCode === 404) {
            // return 404 component
         }
          if (this.state.errorType.statusCode === 403) {
            // return 403 component
         }
         if (this.state.errorType.statusCode === 400) {
            // return 400 component
         }
          if (this.state.errorType.statusCode === 500) {
            // return 500 component
         }
         // return generic message here
      }
        return <h1>Uh-oh! Cannot render app, check console.</h1>;
    }

    return this.props.children;
  }
}

Thanks a lot Niels