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);
}
}
I believe that you should enable this application properties in application configuration.
server:
error:
include-message: always
More details here.
I would recommend a more scientific approach where you design error models based on what will work best in UI displays and API logs.
AXIOS IN THE CLIENT
You can't rely on always receiving an error response body. Eg sometimes you may get an empty 502 bad gateway error.
Write code defensively, and in all cases translate to the UI's error model. A possible way of coding in this example code.
SPRING IN THE SERVER
Again define an error payload, eg with a code and message or whatever your company thinks will work best. Exception filters will enable you to control the response format.
In Spring there can be multiple such filters. You may be happy with Spring's default error format, but you are not limited to it. Spring is an extensible franework that you can customize as required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With