Skip to content

Commit 6d44351

Browse files
Introduction of RestExceptionHandler.java
1 parent 42a0203 commit 6d44351

File tree

13 files changed

+128
-136
lines changed

13 files changed

+128
-136
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.javadiscord.javabot.api.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
/**
6+
* Represents a single API-Error which holds the {@link HttpStatus}, a message and
7+
* an array of errors.
8+
*
9+
* @param status The {@link HttpStatus}.
10+
* @param message The errors' message.
11+
* @param errors An array of additional error notices.
12+
*/
13+
public record ApiError(HttpStatus status, String message, String... errors) {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.javadiscord.javabot.api.exception;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.lang.Nullable;
5+
6+
/**
7+
* An exception which is thrown for invalid JDA entity ids.
8+
*/
9+
public class InternalServerException extends BeansException {
10+
11+
public InternalServerException(String msg, Throwable cause) {
12+
super(msg, cause);
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.javadiscord.javabot.api.exception;
2+
3+
import org.springframework.beans.BeansException;
4+
import org.springframework.lang.Nullable;
5+
6+
/**
7+
* An exception which is thrown for invalid JDA entity ids.
8+
*/
9+
public class InvalidEntityIdException extends BeansException {
10+
@Nullable
11+
private final Class<?> requiredEntity;
12+
13+
public InvalidEntityIdException(Class<?> requiredEntity, String msg) {
14+
super(msg);
15+
this.requiredEntity = requiredEntity;
16+
}
17+
18+
public Class<?> getRequiredEntity() {
19+
return requiredEntity;
20+
}
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.javadiscord.javabot.api.exception;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
9+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
10+
11+
import java.sql.SQLException;
12+
13+
/**
14+
* Handles all Rest Exceptions.
15+
*/
16+
@ControllerAdvice
17+
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
18+
19+
/**
20+
* Handles all {@link MethodArgumentTypeMismatchException}s.
21+
*
22+
* @param e The {@link MethodArgumentTypeMismatchException} which was thrown.
23+
* @return The {@link ResponseEntity} containing the {@link ApiError}.
24+
*/
25+
@ExceptionHandler({ MethodArgumentTypeMismatchException.class })
26+
public ResponseEntity<ApiError> handleMethodArgumentTypeMismatch(@NotNull MethodArgumentTypeMismatchException e) {
27+
ApiError error = new ApiError(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),
28+
String.format("%s should be of type %s", e.getName(), e.getRequiredType() != null ? e.getRequiredType().getName() : ""));
29+
return new ResponseEntity<>(error, error.status());
30+
}
31+
32+
/**
33+
* Handles all {@link InvalidEntityIdException}s.
34+
*
35+
* @param e The {@link InvalidEntityIdException} which was thrown.
36+
* @return The {@link ResponseEntity} containing the {@link ApiError}.
37+
*/
38+
@ExceptionHandler({ InvalidEntityIdException.class })
39+
public ResponseEntity<ApiError> handleInvalidEntityIdException(@NotNull InvalidEntityIdException e) {
40+
ApiError error = new ApiError(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(), "Entity should be of type: " + e.getRequiredEntity().getName());
41+
return new ResponseEntity<>(error, error.status());
42+
}
43+
44+
/**
45+
* Handles all {@link SQLException}s.
46+
*
47+
* @param e The {@link SQLException} which was thrown.
48+
* @return The {@link ResponseEntity} containing the {@link ApiError}.
49+
*/
50+
@ExceptionHandler({ InternalServerException.class })
51+
public ResponseEntity<ApiError> handleInternalServerException(@NotNull InternalServerException e) {
52+
ApiError error = new ApiError(HttpStatus.BAD_REQUEST, e.getCause().getLocalizedMessage(), e.getLocalizedMessage());
53+
return new ResponseEntity<>(error, error.status());
54+
}
55+
}

src/main/java/net/javadiscord/javabot/api/gson/GsonColorAdapter.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/net/javadiscord/javabot/api/gson/GsonLocalDateTimeAdapter.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/net/javadiscord/javabot/api/response/ApiResponseBuilder.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/main/java/net/javadiscord/javabot/api/response/ApiResponses.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/net/javadiscord/javabot/api/routes/metrics/MetricsController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.entities.Guild;
77
import net.javadiscord.javabot.Bot;
8-
import net.javadiscord.javabot.api.response.ApiResponses;
8+
import net.javadiscord.javabot.api.exception.InvalidEntityIdException;
99
import net.javadiscord.javabot.api.routes.CaffeineCache;
1010
import net.javadiscord.javabot.api.routes.metrics.model.MetricsData;
1111
import net.javadiscord.javabot.data.config.guild.MetricsConfig;
@@ -49,10 +49,10 @@ public MetricsController(final JDA jda) {
4949
@GetMapping(
5050
value = "{guild_id}/metrics"
5151
)
52-
public ResponseEntity<?> getMetrics(@PathVariable(value = "guild_id") long guildId) {
52+
public ResponseEntity<MetricsData> getMetrics(@PathVariable(value = "guild_id") long guildId) {
5353
Guild guild = jda.getGuildById(guildId);
5454
if (guild == null) {
55-
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
55+
throw new InvalidEntityIdException(Guild.class, "You've provided an invalid guild id!");
5656
}
5757
MetricsData data = getCache().getIfPresent(guild.getIdLong());
5858
if (data == null) {

src/main/java/net/javadiscord/javabot/api/routes/qotw_leaderboard/QOTWLeaderboardController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.entities.Guild;
66
import net.javadiscord.javabot.Bot;
7-
import net.javadiscord.javabot.api.response.ApiResponses;
7+
import net.javadiscord.javabot.api.exception.InvalidEntityIdException;
88
import net.javadiscord.javabot.api.routes.CaffeineCache;
99
import net.javadiscord.javabot.api.routes.qotw_leaderboard.model.QOTWMemberData;
1010
import net.javadiscord.javabot.systems.qotw.QOTWPointsService;
@@ -53,13 +53,13 @@ public QOTWLeaderboardController(final JDA jda) {
5353
value = "{guild_id}/qotw/leaderboard",
5454
produces = MediaType.APPLICATION_JSON_VALUE
5555
)
56-
public ResponseEntity<?> getQOTWLeaderboard(
56+
public ResponseEntity<List<QOTWMemberData>> getQOTWLeaderboard(
5757
@PathVariable(value = "guild_id") long guildId,
5858
@RequestParam(value = "amount", defaultValue = "3") int amount
5959
) {
6060
Guild guild = jda.getGuildById(guildId);
6161
if (guild == null) {
62-
return new ResponseEntity<>(ApiResponses.INVALID_GUILD_IN_REQUEST, HttpStatus.BAD_REQUEST);
62+
throw new InvalidEntityIdException(Guild.class, "You've provided an invalid guild id!");
6363
}
6464
QOTWPointsService service = new QOTWPointsService(Bot.getDataSource());
6565
List<QOTWMemberData> members = getCache().getIfPresent(guild.getIdLong());

0 commit comments

Comments
 (0)