使用try和catch异步方法的正确方法是什么?

时间:2018-08-17 20:04:15

标签: javascript typescript promise try-catch

我有来自客户端的请求,我可以在尝试捕获之前打印出request.body,但是它没有进入try块,从而阻止了所有已实现的底层链接,任何想法我在此花费了数小时都无法实现找到问题。

main.ts

export class OrderController extends Controller {

    private _request: IRequestURL[] = [];

    constructor() {
        super();
    }

    public async processOrderSummary(@Request() request: ExpressRequest,
        response: ExpressResponse) {
        console.log("OrderCOntroll>>>>>>>", request.body);

        try {
            const data = await this.processOrderSummaryRequest(request);
            if (!_.isEmpty(data)) {
                response.send(data);
            }
        } catch (err) {
            response.send(err);
            console.log("ERR_POS", err);
        }
    }

    private async processOrderSummaryRequest(request: ExpressRequest): Promise < any > {

        const bRetSpecialty: boolean = await this.specialtyOrderSummaryRequest(request);
        const bRetPBM: boolean = await this.pmbOrderSummaryRequest(request);
        if (!bRetPBM && !bRetSpecialty) {
            return Promise.reject(new Error("Processing failed"));
        }
        const __data: IResponse = await makeRequest(this._request);
        if (__data.Headers.StatusCode !== REQUEST_SUCCESS_STATUS_CODE) {

            // some proecessing
            // check to see which request failed
            if (!_.isEmpty(__data.Headers.IndexOfRequestThatFailed)) {
                // some other processing
            }
        }

        let data = await this.specialtyprocessOrderSummaryResponse(__data.Details[0]);
        data += await this.pbmprocessOrderSummaryResponse(__data.Details[1]);

        return data;
    }

    @Post('getOrderSummary')
    private async specialtyOrderSummaryRequest(@Body() request: ExpressRequest): Promise < boolean > {

        return new Promise < boolean > ((resolve) => {
            const specialtyUrl = urlConfig.specialtyBaseUrl + urlConfig.specialtyRoute + "Order/getOrderSummary";

            const reqObject: IRequestURL = {}
            as IRequestURL;
            reqObject.url = specialtyUrl;
            reqObject.body = request.body;

            return resolve(true);
        });
    }

0 个答案:

没有答案