diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java index 8fe59a846e..86698013b2 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithError.java @@ -12,6 +12,14 @@ import java.text.MessageFormat; public enum AppsmithError { // Ref syntax for message templates: // https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/MessageFormat.html + HTTP_METHOD_NOT_ALLOWED( + 405, + AppsmithErrorCode.HTTP_METHOD_NOT_ALLOWED.getCode(), + "HTTP Method not allowed.", + AppsmithErrorAction.DEFAULT, + "Invalid method", + ErrorType.BAD_REQUEST, + null), INVALID_PARAMETER( 400, AppsmithErrorCode.INVALID_PARAMETER.getCode(), diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java index b4e96785db..a36f37bcf3 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/AppsmithErrorCode.java @@ -4,6 +4,7 @@ import lombok.Getter; @Getter public enum AppsmithErrorCode { + HTTP_METHOD_NOT_ALLOWED("AE-APP-4001", "HTTP method not allowed"), INVALID_ACTION_COLLECTION("AE-ACC-4038", "Invalid action collection"), UNAUTHORIZED_ACCESS("AE-ACL-4003", "Unauthorized access"), ACL_NO_RESOURCE_FOUND("AE-ACL-4004", "Acl no resource found"), diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java index 4c6d545431..d20892daba 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java @@ -17,6 +17,7 @@ import io.micrometer.core.instrument.util.StringUtils; import io.sentry.Sentry; import io.sentry.SentryLevel; import io.sentry.protocol.User; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.errors.LockFailedException; @@ -28,6 +29,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.support.WebExchangeBindException; +import org.springframework.web.server.MethodNotAllowedException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import reactor.core.publisher.Mono; @@ -44,6 +46,7 @@ import java.util.Map; * sending it to the client. */ @ControllerAdvice +@RequiredArgsConstructor @Slf4j public class GlobalExceptionHandler { @@ -55,17 +58,6 @@ public class GlobalExceptionHandler { private final SessionUserService sessionUserService; - public GlobalExceptionHandler( - RedisUtils redisUtils, - AnalyticsService analyticsService, - GitFileUtils fileUtils, - SessionUserService sessionUserService) { - this.redisUtils = redisUtils; - this.analyticsService = analyticsService; - this.fileUtils = fileUtils; - this.sessionUserService = sessionUserService; - } - private void doLog(Throwable error) { if (error instanceof BaseException baseException && baseException.isHideStackTraceInLogs()) { log.error(baseException.getClass().getSimpleName() + ": " + baseException.getMessage()); @@ -284,6 +276,25 @@ public class GlobalExceptionHandler { return getResponseDTOMono(urlPath, response); } + @ExceptionHandler + @ResponseBody + public Mono> catchMethodNotAllowed(MethodNotAllowedException e, ServerWebExchange exchange) { + AppsmithError appsmithError = AppsmithError.HTTP_METHOD_NOT_ALLOWED; + + exchange.getResponse().setStatusCode(HttpStatus.resolve(appsmithError.getHttpErrorCode())); + + String urlPath = exchange.getRequest().getPath().toString(); + ResponseDTO response = new ResponseDTO<>( + appsmithError.getHttpErrorCode(), + new ErrorDTO( + appsmithError.getAppErrorCode(), + appsmithError.getErrorType(), + appsmithError.getMessage(e.getMessage()), + appsmithError.getTitle())); + + return getResponseDTOMono(urlPath, response); + } + /** * This function catches the generic Exception class and is meant to be a catch all to ensure that we don't leak * any information to the client. Ideally, the function #catchAppsmithException should be used