Issue
I have a function that sends an axios
rrequest to my springboot backend.
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;
});
}
For some reason, when I run the project locally, I am able to get the error message above, but when I deploy the project to production, the error message is undefined
.
Example of the above print statement^:
Error message when ran locally: the error: Email must be a work email. The email zx is NOT allowed.
Error message when deployed: the error: undefined
Code from my backend: The controller:
@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);
}
So if you look that the above function, you can see that when I run the project locally, I can get the NotAllowedException
to be thrown back to my frontend, but not when deplpyed.
I read online that it could be due to a CORS header issue, but I don't think so because this is my CORS
configuration and it still isn't working:
@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;
}
This is an example of one of my custom Exception classes:
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);
}
}
Solution
I believe that you should enable this application properties in application configuration.
server:
error:
include-message: always
More details here.
Answered By - Vinh Truong
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.