【问题标题】:How to pass error back to caller function using async?如何使用异步将错误传递回调用者函数?
【发布时间】:2018-11-07 16:01:48
【问题描述】:

如果没有找到刚刚将空对象发送回客户端的“dateofBirth”,我现在会抛出错误,我如何将错误对象而不是空对象传递回客户端。基本上是想发回进程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);
            }

        }

        // private _request: IRequestURL[] = [];

        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);
            }
            let proxyMember: ISpecialtyInfoObj = {}
            as ISpecialtyInfoObj;
            for (const member of specialtyMembers) {
                if (member.specialtyIdEnc === request.body.getSpecialtyQuestionsRequest.details.specialtyIdEnc) {
                    proxyMember = member;
                    if (!member.dateOfBirth) {
                        throw new Error('no patient info for given HBS ID');
                    }
                    break;
                }
            }

        }

【问题讨论】:

  • 我试过它仍然返回空对象 {} 到 UI 并在网络响应中显示错误

标签: javascript angular typescript error-handling


【解决方案1】:

创建自定义异常对象以在抛出错误时传递数据

class CustomError extends Error {
  constructor(foo = 'bar', ...params) {
    // Pass remaining arguments (including vendor specific ones) to parent constructor
    super(...params);

    // Maintains proper stack trace for where our error was thrown (only available on V8)
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, CustomError);
    }

    // Custom debugging information
    this.foo = foo;
    this.date = new Date();
  }
}

try {
  throw new CustomError('baz', 'bazMessage');
} catch(e){
  console.log(e.foo); //baz
  console.log(e.message); //bazMessage
  console.log(e.stack); //stacktrace
}

更多参考来自官方 mdn 文档 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

【讨论】:

  • 我想发送像return { header: { statusCode: REQUEST_SYSTEM_FAILURE, statusDesc: REQUEST_FAILURE_STATUS_DESCRIPTION, refID: request.body.getSpecialtyQuestionsRequest.header.serviceContext.refID, tokenID: request.body.getSpecialtyQuestionsRequest.header.serviceContext.tokenID }这样的对象
  • 您要将对象发送到哪里?将您的对象设置在throw new CustomError(YOUR_CUSTOM_OBJECT, 'bazMessage'); 中,并在父函数的捕获中获取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2014-12-01
相关资源
最近更新 更多