-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rest-api) improve error handling for 415 unsupported media type #…
…30828 (#30910) ### Proposed Changes * Added `NotSupportedExceptionMapper` to handle `javax.ws.rs.NotSupportedException` and return JSON responses for `415 Unsupported Media Type` errors. * Included new test cases in the Postman collection to validate proper handling and responses for requests with invalid `Content-Type` headers. ### Checklist - [x] Tests - [x] Translations - [x] Security Implications Contemplated (add notes if applicable) ### Additional Info This PR addresses the issue where REST endpoints annotated with `@Consumes(multipart/form-data)` would return HTML responses instead of JSON when the `Content-Type` header is incorrect. The new implementation ensures consistency and better user experience by providing meaningful JSON error messages across the application. ### Screenshots Original | Updated :-------------------------:|:-------------------------: <img width="1017" alt="image" src="https://github.com/user-attachments/assets/a20e9cc5-3887-444c-8ed4-257c1cb60ecc"> | <img width="961" alt="image" src="https://github.com/user-attachments/assets/768f6e72-9160-436e-8a6b-e39972b11260">
- Loading branch information
1 parent
2e4664f
commit d67b5fd
Showing
2 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
dotCMS/src/main/java/com/dotcms/rest/exception/mapper/NotSupportedExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.dotcms.rest.exception.mapper; | ||
|
||
import com.dotmarketing.util.Logger; | ||
|
||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* Exception mapper for handling NotSupportedException. | ||
* <p> | ||
* This class maps the javax.ws.rs.NotSupportedException to a proper HTTP response. | ||
* It logs the warning and returns a response with a status of UNSUPPORTED_MEDIA_TYPE. | ||
* </p> | ||
*/ | ||
@Provider | ||
public class NotSupportedExceptionMapper implements ExceptionMapper<javax.ws.rs.NotSupportedException> { | ||
|
||
/** | ||
* Converts a NotSupportedException into an HTTP response. | ||
* | ||
* @param exception The NotSupportedException that was thrown. | ||
* @return A Response object containing the error message and a status of UNSUPPORTED_MEDIA_TYPE. | ||
*/ | ||
@Override | ||
public Response toResponse(javax.ws.rs.NotSupportedException exception) { | ||
|
||
Logger.warn(this.getClass(), exception.getMessage(), exception); | ||
return ExceptionMapperUtil.createResponse(ExceptionMapperUtil.getJsonErrorAsString(exception.getMessage()), | ||
exception.getMessage(), Response.Status.UNSUPPORTED_MEDIA_TYPE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters