Skip to content

Commit

Permalink
Change tag value because having space in it is not compliant. Update …
Browse files Browse the repository at this point in the history
…code examples to better match rule implementation
  • Loading branch information
irina-batinic-sonarsource committed Dec 11, 2023
1 parent 75539f5 commit 00aae6b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rules/S6863/java/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"tags": [
"spring",
"best practice"
"best-practice"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6863",
Expand Down
11 changes: 3 additions & 8 deletions rules/S6863/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The request handler function in a `Controller` should set the appropriate HTTP s
This is done by returning a `Response` object with the appropriate status code.

If an exception is thrown during the execution of the handler, the status code should be in the range of 4xx or 5xx.
If no exception is thrown, the status code should be in the range of 2xx or 4xx.
If no exception is thrown, the status code should be in the range of 2xx.

== How to fix it

Expand All @@ -20,10 +20,8 @@ public class UserController {
try {
User user = userService.getUserById(userId);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(user); // Noncompliant: Setting 500 for a successful operation
} catch (NotFoundException e) {
return ResponseEntity.status(HttpStatus.OK).build(); // Noncompliant: Set 200 for resource not found
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // Noncompliant: Set 404 for other exceptions
return ResponseEntity.status(HttpStatus.OK).build(); // Noncompliant: Set 200 for exception
}
}
}
Expand All @@ -35,15 +33,12 @@ public class UserController {
----
@Controller
public class UserController {
public ResponseEntity<User> getUserById(Long userId) {
try {
User user = userService.getUserById(userId);
return ResponseEntity.ok(user); // Compliant: Set 200 for success
} catch (NotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // Compliant: Set 404 for resource not found
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); // Compliant: Set 500 for other exceptions
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); // Compliant: Set 400 or 500 for exceptions
}
}
}
Expand Down

0 comments on commit 00aae6b

Please sign in to comment.