I am new to APIs and don't know if I should do it, for example, if one of the fields during creation was not validated I throw InvalidRequestException with message ('field' has 'problem description' ).
Asked
Active
Viewed 53 times
0
-
If you are asking for "permission" - https://stackoverflow.com/questions/6086334. If you are asking for advice as to when you should do it ... well the answer is (IMO) pretty much the same as whether / when a method should throw and exception (and what kind of exception is appropriate). – Stephen C Aug 20 '21 at 13:00
-
You mean your Web API? Or some Java Service API? – Benjamin M Aug 20 '21 at 13:03
-
@BenjaminM - Well Web APIs don't throw exceptions .... – Stephen C Aug 20 '21 at 13:04
-
@StephenC Sure it does, but get catched by Spring's Exception Handler and then is getting transformed to some desired HTTP output (for example JSON). See: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc or https://www.baeldung.com/exception-handling-for-rest-with-spring – Benjamin M Aug 20 '21 at 13:05
-
But that isn't the Web API. That's the Java API. The Web API is the HTTP request / responses. Or at least, that is how that term is *normally* used; see https://en.wikipedia.org/wiki/Web_API – Stephen C Aug 20 '21 at 13:09
-
you can probably return 400 Bad request instead of an exception – vaibhavsahu Aug 20 '21 at 13:19