Axios 捕获的错误消息在生产中未定义,但在本地有效?

Error message caught by Axios is undefined in production, but works locally?

我有一个函数可以向我的 springboot 后端发送 axios rrequest。

  async createUser(user) {
    return axios.post(API_BASE_URL + "users/register", user).catch((error) => {
      console.log("the error: " + error.response.data.message);
      throw error.response.data.message;
    });
  }

出于某种原因,当我在本地 运行 项目时,我能够得到上面的错误消息,但是当我将项目部署到生产环境时,错误消息是 undefined

上面打印语句的例子^:

本地 运行 时的错误消息:the error: Email must be a work email. The email zx is NOT allowed.

部署时的错误消息:the error: undefined

来自我后端的代码: 控制器:

    @PostMapping("/api/users/register")
    public User registerUser(@RequestBody User user) {
        return userServices.createUser(user);
    }
    public User createUser(User userDetails) {
        if (Boolean.TRUE.equals(emailExists(userDetails.getEmail()))) {
            throw new DuplicateResourceException(
                    "There is already an account with that email address: " +
                            userDetails.getEmail());
        }
        if (!userDetails.getEmail().toLowerCase().endsWith("@company.com")) {
            throw new NotAllowedException(
                    "Email must be a work email. The email " +
                            userDetails.getEmail() + " is NOT allowed.");
        }
        if (!userDetails.getCompanyPasscode().equals(env.getProperty("company.passcode"))) {
            throw new NotAllowedException(
                    "Incorrect company passcode");
        }
        User user = new User();
        user.setEmail(userDetails.getEmail());
        user.setPassword(passwordEncoder.encode(userDetails.getPassword()));
        return userRepo.save(user);
    }

所以如果你看上面的函数,你可以看到当我在本地 运行 项目时,我可以得到 NotAllowedException 被扔回我的前端,但不是 deplpyed .

我在网上看到这可能是由于 CORS header 问题,但我不这么认为,因为这是我的 CORS 配置,但它仍然无法正常工作:

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration config = new CorsConfiguration();

        config.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
        config.setAllowedHeaders(Arrays.asList("*"));
        config.setAllowedOrigins(Arrays.asList("*"));

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;

    }

这是我的自定义异常之一的示例 类:

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class NotAllowedException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public NotAllowedException(String message) {
        super(message);
    }
}

通过搜索,我发现一些人说他们在两个系统上的 Java 版本不同时遇到了这个问题。

我认为您应该在应用程序配置中启用此应用程序属性。

server:
  error:
  include-message: always

更多详情here

我会推荐一种更科学的方法,您可以根据在 UI 显示和 API 日志中最有效的方法设计错误模型。

客户端中的 AXIOS

您不能指望总是收到错误响应正文。例如,有时您可能会收到空的 502 错误网关错误。

防御性地编写代码,并在所有情况下转换为 UI 的错误模型。 this example code.

中的一种可能的编码方式

SPRING 在服务器中

再次定义错误负载,例如使用 codemessage 或您公司认为最有效的任何内容。 Exception filters 将使您能够控制响应格式。

在 Spring 中可以有多个这样的过滤器。您可能对 Spring 的默认错误格式感到满意,但不限于此。 Spring 是一个可扩展的框架,您可以根据需要进行自定义。