代码之家  ›  专栏  ›  技术社区  ›  user824624

错误处理在express中输出html而不是json

  •  1
  • user824624  · 技术社区  · 1 年前

    我按照express.js的教程制作了一个简单的错误处理程序

    function clientErrorHandler(err, req, res, next) {
      if (req.xhr) {
        console.log('clienterrorhandler', err);
        res.status(500).send({ error: 'Something failed!' });
      } else {
        next(err);
      }
    }
    
    app.use(clientErrorHandler);
    

    这就是我定义错误的方式

    import { StatusCodes } from 'http-status-codes';
    import CustomAPIError from './custom-errors';
    
    class UnauthenticatedError extends CustomAPIError {
      constructor(
        public message: string,
        public statusCode: number,
      ) {
        super(message, statusCode);
        this.statusCode = StatusCodes.UNAUTHORIZED;
      }
    }
    export default UnauthenticatedError;
    

    身份验证中间件

    export const auth = asyncHandler(
      async (
        req: AuthenticatedRequest,
        res: Response,
        next: NextFunction,
      ): Promise<void> => {
        try {
        } catch (err) {
          throw new UnauthenticatedError(
            'There is no token atached to the Header.',
            StatusCodes.UNAUTHORIZED,
          );
        }
      },
    );
    

    关键问题是,当错误发生时,客户端会收到一个基于html的错误,而我的错误处理程序无法解决这个问题

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Error</title>
    </head>
    <body>
    <pre>Error: Not Athorized token expired, Please Login again.<br> &nbsp; &nbsp;at authMiddleware.ts:44:15<br> &nbsp; &nbsp;at asyncUtilWrap 
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Phil    1 年前

    你依赖 req.xhr 领域仅当请求包含标头时才设置此项

    X-Requested-With: XMLHttpRequest
    

    从内存中,只有jQuery的 $.ajax() 默认情况下设置此选项;Axios/ XMLHttpRequest fetch() 不要。

    您应该使用 content negotiation 通过 Accept 头球

    app.use((err, req, res, next) => {
      if (req.accepts('application/json')) {
        console.log('clienterrorhandler', err);
        return res.status(500).json({ error: 'Something failed!' });
      }
      return next(err);
    });
    

    和客户端

    fetch(url, {
      headers: {
        Accept: 'application/json',
      },
    });
    
    推荐文章