【问题标题】:Is it possible to modify error messages based on the status of the error?是否可以根据错误状态修改错误消息?
【发布时间】:2019-11-30 04:23:42
【问题描述】:

是否有可能如何覆盖发生错误时显示的那些临时错误消息?例如,当500 状态出现错误时,front-end 会显示错误:Internal Server Error。我想检查错误的状态是否为 500,然后将消息覆盖为更具体的内容。 到目前为止,我已尝试将此代码放入我的AuthProvider.js,但它似乎对我不起作用。

if (type === AUTH_ERROR) {
  const status = params.status;
  if (status === 500) {
    throw new Error('ErrorMessage');
  }
  return Promise.resolve();
}

我检查了status 的值,它是 500。但消息保持不变。

有什么办法解决这个问题吗?

提前谢谢你。

【问题讨论】:

    标签: reactjs error-handling react-admin


    【解决方案1】:

    如果您的目的是在控制台日志中观察精确的消息,您可以执行以下操作:

    {
        console.error('Auth failed');
    }
    

    由于您澄清我错误地假设了您的目的,以下是满足您抛出自定义错误请求的代码:

    function CommonException(message, code)
    {
        this.message = message;
        this.code = code;
    }
    
    try
    {
        var exception = new CommonException('ErrorMessage', 500);
    
        throw exception;
    
        alert('Whatever');
    }
    catch (e) {
        alert(e.message);
        alert(e.code)
    }
    

    【讨论】:

    • 没有。我想检查错误的状态是否为 500,然后将消息覆盖为更具体的内容。这就是我想要实现的,而不是console.log 变量或字符串。
    • 另外,如果你的意思是你只想扩展错误,请检查这个答案:stackoverflow.com/questions/31089801/…
    猜你喜欢
    • 2019-01-20
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 2018-12-18
    • 2016-11-07
    • 2020-02-17
    相关资源
    最近更新 更多