2

I have created a nodejs/express api, which is a wrapper around another api.

I am making async calls with try and catch. I am wondering in the inner async call if it catches an error will this error be caught by the outer async call.

The transaction.controller is my api which calls the transaction.repository to an external api. The function getTransactionResult is throwing an error in the catch part. However, this does NOT loop back to the catch part of my show function.

router.ts

this.router.get('/:id', this.controller.show.bind(this.controller));

transaction.controller.ts

public async show(req: Request, res: Response): Promise<any> {

        const params = req.params;
        const token = req.headers.token;

        let transaction;

        try {
            transaction = await this.transactionRepo.getTransactionResult(params.id, token);

            console.log("instead carries on here"); // 2

            if (transaction.success === false) {
                return res.status(404).json({
                    success: false,
                    status: 404,
                    data: transaction,
                    message: "Failed to retrieve transaction result"
                });
            }

            return res.status(200).json({
                success: true,
                status: 200,
                data: transaction,
                message: "Successfully retrieved transaction result"
            });

        } catch (err) {

            //console.log("Does not call this");
            return res.status(500).json({
                success: false,
                status: 400,
                data: err,
                message: "The server threw an unexpected error",
            });
        }

transaction.repository.ts

public async getTransactionResult(transactionId: string, token: string) {

        const url = this.config.api_url + "/api/transactions/" + transactionId + "/summary";

        const options = {
            uri: url,
            headers: {
                'token': token
            },
            body: {

            },
            json: true,
            resolveWithFullResponse: true
        }


        let result;
        try {

            result = await request.get(options);

            if (result.statusCode !== 200) {
                return { "success": false, data: result }
            }

            return { "success": true, data: result }

        } catch (err) {
            console.log("CAUGHT ERROR"); // 1
            return err;
        }

    }
1
  • Since you are handling error in catch block in getTransactionResult(), it won't be propagated to outer async call inside show(). Commented Jun 21, 2018 at 9:54

1 Answer 1

2

You need to rethrow the error not return it.

By doing return err you are resolving the Promise which mean the async operation succeeded. Hence why your out try-catch in transaction.controller.ts does not catch anything.

Either:

  1. Don't catch the error and let it bubble up

  2. Rethrow the error

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.