Skip to content

Commit 1503860

Browse files
committed
Fix status code errors
1 parent 2158bef commit 1503860

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

common/src/main/java/net/modfest/platform/pojo/PlatformErrorResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import com.google.gson.JsonElement;
44
import org.jspecify.annotations.NonNull;
5+
import org.jspecify.annotations.Nullable;
56

67
/**
78
* The standard way that platform returns errors
89
*/
910
public record PlatformErrorResponse(
1011
@NonNull ErrorType type,
11-
@NonNull JsonElement data
12+
@NonNull JsonElement data,
13+
@Nullable Integer overrideStatusCode
1214
) {
1315
public enum ErrorType {
1416
/**

platform_api/src/main/java/net/modfest/platform/controller/PlatformExceptionHandler.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@ public class PlatformExceptionHandler {
3030
private ResponseEntity<PlatformErrorResponse> platformException(PlatformStandardException t) {
3131
return toResponse(new PlatformErrorResponse(
3232
t.getType(),
33-
gson.toJsonTree(t.getData())
33+
gson.toJsonTree(t.getData()),
34+
t.getStatusCodeOverride()
3435
));
3536
}
3637

3738
@ExceptionHandler(UnauthenticatedException.class)
3839
private ResponseEntity<PlatformErrorResponse> unauthenticatedException(UnauthenticatedException e) {
3940
return toResponse(new PlatformErrorResponse(
4041
PlatformErrorResponse.ErrorType.PERMISSION_ERROR,
41-
gson.toJsonTree("user is not logged in")
42+
gson.toJsonTree("user is not logged in"),
43+
null
4244
));
4345
}
4446

4547
@ExceptionHandler({AuthorizationException.class, AuthenticationException.class})
4648
private ResponseEntity<PlatformErrorResponse> authorizationException(ShiroException e) {
4749
return toResponse(new PlatformErrorResponse(
4850
PlatformErrorResponse.ErrorType.PERMISSION_ERROR,
49-
gson.toJsonTree(e.toString())
51+
gson.toJsonTree(e.toString()),
52+
null
5053
));
5154
}
5255

@@ -59,22 +62,24 @@ private ResponseEntity<PlatformErrorResponse> anyError(Throwable t) {
5962
return new ResponseEntity<>(
6063
new PlatformErrorResponse(
6164
PlatformErrorResponse.ErrorType.INTERNAL,
62-
gson.toJsonTree(e.getBody())
65+
gson.toJsonTree(e.getBody()),
66+
null
6367
),
6468
e.getStatusCode()
6569
);
6670
}
6771
t.printStackTrace();
6872
return toResponse(new PlatformErrorResponse(
6973
PlatformErrorResponse.ErrorType.INTERNAL,
70-
gson.toJsonTree(t.toString())
74+
gson.toJsonTree(t.toString()),
75+
null
7176
));
7277
}
7378

7479
private static ResponseEntity<PlatformErrorResponse> toResponse(PlatformErrorResponse errorResponse) {
7580
return new ResponseEntity<>(
7681
errorResponse,
77-
HttpStatusCode.valueOf(errorResponse.type().httpStatus)
82+
HttpStatusCode.valueOf(errorResponse.overrideStatusCode() == null ? errorResponse.type().httpStatus : errorResponse.overrideStatusCode())
7883
);
7984
}
8085
}

platform_api/src/main/java/net/modfest/platform/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public UserData getSingleUser(@PathVariable String id) throws PlatformStandardEx
123123
}
124124

125125
if (user == null) {
126-
throw PlatformStandardException.doesntExist(PlatformErrorResponse.IdType.MFUSER, id);
126+
throw PlatformStandardException.doesntExist(PlatformErrorResponse.IdType.MFUSER, id).statusCode(404);
127127
}
128128
return user;
129129
}

platform_api/src/main/java/net/modfest/platform/misc/PlatformStandardException.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class PlatformStandardException extends Exception {
88
private final PlatformErrorResponse.ErrorType type;
99
private final Object data;
10+
private Integer statusCodeOverride = null;
1011

1112
public PlatformStandardException(PlatformErrorResponse.ErrorType type, Object data) {
1213
this.type = type;
@@ -18,6 +19,11 @@ public String getMessage() {
1819
return this.type+": "+this.data;
1920
}
2021

22+
public PlatformStandardException statusCode(int code) {
23+
statusCodeOverride = code;
24+
return this;
25+
}
26+
2127
public static PlatformStandardException doesntExist(PlatformErrorResponse.IdType type, String id) {
2228
return new PlatformStandardException(PlatformErrorResponse.ErrorType.DOESNT_EXIST, new PlatformErrorResponse.DoesntExist(type, id));
2329
}

0 commit comments

Comments
 (0)