Skip to main content

10.07 Handling Errors

New withErrorHandler.js

withErrorHandler.js
import React, { Component, Fragment } from "react";
import Modal from "../../components/UI/Modal/Modal";

const withErrorHandler = (WrapperComponent, axios) => {
return class extends Component {
state = {
error: null,
};
componentDidMount() {
axios.interceptors.request.use((req) => {
this.setState({ error: null });
return req;
});
axios.interceptors.response.use(
(res) => res,
(error) => {
this.setState({ error: error });
}
);
}

errorConfirmedHandler = () => {
this.setState({ error: null });
};

render() {
return (
<Fragment>
<Modal
show={this.state.error}
modalClosed={this.errorConfirmedHandler}
>
{this.state.error ? this.state.error.message : null}
</Modal>
<WrapperComponent {...this.props} />;
</Fragment>
);
}
};
};

export default withErrorHandler;

BurgerBuilder.js

BurgerBuilder.js
import React, { Component, Fragment } from "react";
import axios from "../../axios-orders";
import BuildControls from "../../components/Burger/BuildControls/BuildControls";
import Burger from "../../components/Burger/Burger";
import OrderSummary from "../../components/Burger/OrderSummary/OrderSummary";
import Modal from "../../components/UI/Modal/Modal";
import Spinner from "../../components/UI/Spinner/Spinner";
import withErrorHandler from "../../hoc/withErrorHandler/withErrorHandler";

const INGREDIENT_PRICES = {
salad: 0.5,
cheese: 0.4,
meat: 1.3,
bacon: 0.7,
};

...

export default withErrorHandler(BurgerBuilder, axios);