接口400返回错误信息

service.interceptors.response.use(
  (response) => {
    // if the custom code is not 20000, it is judged as an error.
    if (response.status !== 200) {
      return
    } else {
      return response.data
    }
  },
  (error) => {
    alert(error.response.data.head.msg)
    // console.log('error.respone', error.response.data.head.msg)
    return
  }
)

error.response.data.head.msg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,我们可以定义一个自定义异常类来表示接口返回的错误码或错误信息。下面是一个简单的Java代码示例,用于实现接口返回错误码或错误信息: ``` public class ApiException extends Exception { private int errorCode; public ApiException(int errorCode, String message) { super(message); this.errorCode = errorCode; } public int getErrorCode() { return errorCode; } } ``` 在上面的代码中,我们定义了一个 `ApiException` 异常类,用于表示接口返回的错误码或错误信息。该异常类包含一个错误码 `errorCode` 和错误信息 `message`,并且继承了 `Exception` 类。我们可以在调用接口时,判断接口返回的状态码或错误信息,如果存在错误,则抛出 `ApiException` 异常,如下所示: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ApiClient { private static final String API_URL = "http://www.example.com/api"; public static void main(String[] args) { try { URL url = new URL(API_URL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String response = in.readLine(); in.close(); // 处理响应结果 // ... } else { String errorMsg = conn.getResponseMessage(); throw new ApiException(responseCode, errorMsg); } } catch (ApiException e) { System.out.println("接口返回错误码:" + e.getErrorCode() + ",错误信息:" + e.getMessage()); } catch (Exception e) { System.out.println("其他异常:" + e.getMessage()); } } } ``` 在上面的代码中,当接口返回状态码不是HTTP_OK时,我们可以通过 `conn.getResponseMessage()` 方法获取接口返回错误信息,并抛出 `ApiException` 异常。在 `catch` 块中,我们可以获取异常中的错误码和错误信息,并进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值