Reputation:
My IDE would show the error "missing return statement" within the catch-block when trying to throw a new exception within a method instead of throwing it directly.
Minified sample of the REST controller:
@RestController
public class RootController {
protected final Logger log = Logger.getLogger("myLogger");
@PostMapping(
value={"sample/",
"sample/123"},
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object getFileContentPost (@RequestBody Request payload) {
return checkPayloadAndRespond(payload.getFileName());
}
private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
logErrorAndThrowResponseException(HttpStatus.BAD_REQUEST, "Field fileName is empty or missing!", null);
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
//line causing ERROR "missing return statement":
logErrorAndThrowResponseException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse file!, e);
}
}
private void logErrorAndThrowResponseException(HttpStatus status, String reason, @Nullable Throwable cause) throws ResponseStatusException {
log.error("Error occurred: " + reason + ", returning status: " + status);
throw new ResponseStatusException(status, reason, cause);
}
}
Working sample: if I use the throw new
statement directly without method:
@RestController
public class RootController {
protected final Logger log = Logger.getLogger("myLogger");
@PostMapping(
value={"sample/",
"sample/123"},
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object getFileContentPost (@RequestBody Request payload) {
return checkPayloadAndRespond(payload.getFileName());
}
private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Field fileName is empty or missing!", e);
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse response file!", e);
}
}
}
What is the best way to resolve this?
All I want to do is to actually create a log whenever a ResponseStatusException is thrown.
Thanks in advance.
Upvotes: 0
Views: 203
Reputation:
So I guess the void-return-type is causing the issue.
I resolved it for now by returning an actual response which I then throw:
private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
throw logErrorAndThrowResponseException(HttpStatus.BAD_REQUEST, "Field fileName is empty or missing!", null);
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
throw logErrorAndThrowResponseException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse file!, e);
}
}
private ResponseStatusException logErrorAndThrowResponseException(HttpStatus status, String reason, @Nullable Throwable cause) {
log.error("Error occurred: " + reason + ", returning status: " + status);
return new ResponseStatusException(status, reason, cause);
}
}
Upvotes: 0