chore: Fixing ErrorAttributes null error in logs (#40976)

## Description
> [!TIP]  
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/test sanity

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/15752331180>
> Commit: d1d620a4a82cd89554963dbbcca4844127bb0fc7
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=15752331180&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Sanity`
> Spec:
> <hr>Thu, 19 Jun 2025 08:17:27 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Bug Fixes**
- Improved error handling to ensure more accurate HTTP status codes are
returned for server errors.
- Enhanced logging for cases where the HTTP status cannot be determined.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Trisha Anand 2025-06-20 18:19:29 +05:30 committed by GitHub
parent 1b69f150e2
commit d3d4f0c354
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,10 @@
package com.appsmith.server.exceptions;
import com.appsmith.external.exceptions.ErrorDTO;
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
import com.appsmith.server.dtos.ResponseDTO;
import jakarta.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
@ -30,6 +32,7 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@Component
@Slf4j
@Order(-2)
public class AppSmithErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler {
@ -55,6 +58,39 @@ public class AppSmithErrorWebExceptionHandler extends DefaultErrorWebExceptionHa
return route(all(), this::render);
}
@Override
protected int getHttpStatus(Map<String, Object> errorAttributes) {
Object status = errorAttributes.get("status");
if (status instanceof Integer) {
return (Integer) status;
}
if (status instanceof String) {
try {
return Integer.parseInt((String) status);
} catch (NumberFormatException e) {
log.warn("Could not parse status as integer: {}", status);
}
}
// If status is missing or invalid, check for exception type
Object error = errorAttributes.get("error");
if (error instanceof Throwable) {
Throwable throwable = (Throwable) error;
if (throwable instanceof AppsmithException) {
return ((AppsmithException) throwable).getHttpStatus();
}
if (throwable instanceof AppsmithPluginException) {
return ((AppsmithPluginException) throwable).getHttpStatus();
}
}
// Default to 500 if we can't determine the status
log.warn(
"Could not determine HTTP status from error attributes, defaulting to 500. Error attributes: {}",
errorAttributes);
return 500;
}
@Nonnull
private Mono<ServerResponse> render(ServerRequest request) {
Map<String, Object> error =