如何使用异步将错误从子功能返回到调用者函数?

时间:2018-11-05 03:40:59

标签: javascript angular typescript error-handling try-catch

尝试从execute方法向作为父函数的process抛出错误,如果execute方法出错,则返回空对象。如何将错误从子功能返回到调用者功能,以便它可以使用try/catch将逻辑错误发送回客户端?

main.ts

   export class GetSpecialtyQuestionsController extends Controller {
        public static async process(@Request() request: ExpressRequest,
            response: ExpressResponse): Promise < any > {
            try {
                const instance = new GetSpecialtyQuestionsController();
                const data = await instance.execute(request);
                response.status(200);
                response.send(data);
            } catch (err) {
                response.status(200);
                response.send(err.message);
            }

        }


        constructor() {
            super();
        }

        private async execute(@Request() request: ExpressRequest): Promise < any > {
            // const specialtyMembers = this.getSpecialtyMemberInfoFakeObject();
            const specialtyMembers = await new SpecialtyCacheUtility().getSpecialtyMemberInfoCache(
                request.body.getSpecialtyQuestionsRequest.header.serviceContext.tokenID);
            if (!specialtyMembers) {
                return this.errorHandler(request);
            }
            const body: any = this.buildSingleRequestBody(proxyMember, request.body.getSpecialtyQuestionsRequest);
            const requestObject = this.specialtyQuestionRequest(body);

            const requestArray: IRequestURL[] = [requestObject];

            const data = await makeRequest(requestArray);
            return this.mapSpecialtyResponse(data, specialtyMembers);
        }
    }

0 个答案:

没有答案