如何从nodejs中的常见错误处理程序返回异常

问题描述 投票:0回答:1

我的API在具有分层体系结构的nodejs中。我在从应用程序中的任何位置返回错误时都面临问题。我的快速错误处理程序正在捕获错误,但是随后我的控制器逻辑也正在执行。下面是我的示例代码。

//accountcontroller.ts

updateAccountStatus = async (req: Request, res: Response, next: any): Promise<any> => {
    let responseFromService = await AccountService.updateAccountStatus(
        parseInt(req.params.accountId),
        next
    );

    // even after the exception it comes here, after visiting the express default error code
    if (responseFromService !== undefined) {

       responseHandler(res, 200, responseFromService);
    }

//accountService.ts

  public updateAccountStatus = async (accountId: number, next: any): Promise<any> => {
    let accountStatusReponse = await accountRepository.updateAccountStatus(accountId, next);

    return accountStatusReponse;
  };

//accountRepository.ts

public updateAccountStatus = async (accountId: any, next: any): Promise<any> => {
        try {

        const updateAccountStatus = await Accounts.updateOne(
          { id: accountId },
          { accountStatus: true }
        );
        if (updateAccountStatus) {
          return updateAccountStatus;
        } else {
          throw new Error("Error while saving the data");
        }

    } catch (error) {
      error.err = error.message;
      error.status = 500;
      return next(error);
    }
  };

// responseHandler是我的公共文件,用于返回响应。

responseHandler.ts

class responseHandler {
  constructor() {}
  statusCodeResponse = (
    response: any,
    message: string,
    statusCode: number,
  ): void => {
    response.statusMessage = statusMessage;
    response
      .status(statusCode)
      .json(
        response: response,
        message: message
        )
      );
  };
}

关于如何进行退货的任何建议,以便在出现异常的情况下控件不会移回到上一个文件。

node.js express error-handling es6-promise asynccallback
1个回答
0
投票

在这里:

//accountRepository.ts

public updateAccountStatus = async (accountId: any, next: any): Promise<any> => {
        try {

        const updateAccountStatus = await Accounts.updateOne(
          { id: accountId },
          { accountStatus: true }
        );
        if (updateAccountStatus) {
          return updateAccountStatus;
        } else {
          throw new Error("Error while saving the data");
        }

    } catch (error) {
      error.err = error.message;
      error.status = 500;
      return next(error);
    }
  };

出现错误时,您需要将其重新抛出,以便呼叫者可以看到发生了错误。

//accountRepository.ts

public updateAccountStatus = async (accountId: any, next: any): Promise<any> => {
        try {

        const updateAccountStatus = await Accounts.updateOne(
          { id: accountId },
          { accountStatus: true }
        );
        if (updateAccountStatus) {
          return updateAccountStatus;
        } else {
          throw new Error("Error while saving the data");
        }

    } catch (error) {
      error.err = error.message;
      error.status = 500;
      next(error);
      throw error;                    // throw here so callers see the error
    }
  };
© www.soinside.com 2019 - 2024. All rights reserved.