I am following a standard that requires me to log the class#method that originates an exception for easy debugging from our UI. Without pulling the logs I know what method spawned the exception and can (usually) easily find the root cause.
However, I would like a dynamic way of determining the method signature because I am currently hard-coding it everywhere I am throwing an exception. Is there a better way.
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findUser(Long id){
if(id == null || id < 1){
throw new BadRequestException("UserService#findUser", String.format(
"Invalid Id: %d", id));
}
User user = userRepository.findOne(id);
if(user == null){
throw new DataNotFoundException("UserService#findUser", String.format(
"Can't Find User for Id: %d", id));
}
return user;
}
}
--
The goal is that I can create any custom exception and within that constructor I can infer the method signature by where the exception was created from.
public BadRequestException(String issue){
super(String.format("%s | Bad Request | %s", <<signature>>, issue);
}
--
UserService class --> findUser method --> BadRequest("UserService#findUser")
UserService class --> createUser method --> BadRequest("UserService#createUser")
Aucun commentaire:
Enregistrer un commentaire