fix(api): replace generic 500 errors with specific kubernetes api errors#265
fix(api): replace generic 500 errors with specific kubernetes api errors#265hemantch01 wants to merge 1 commit into
Conversation
…date failures Signed-off-by: hemantch01 <hemantchaudhary905@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Code Review
This pull request enhances error handling across several API endpoints for jobs, queues, and pods by replacing generic 500 errors with specific status codes and descriptive messages based on Kubernetes responses. The feedback suggests refactoring the repeated if-else logic into a more declarative object-based mapping to improve code readability and maintainability across all modified catch blocks.
| let msg; | ||
|
|
||
| if (statusCode === 404) { | ||
| msg = `Job '${req.params.name}' not found in namespace '${req.params.namespace}'.`; | ||
| } else if (statusCode === 409) { | ||
| msg = "Conflict: the job was modified by another process. Please refresh and try again."; | ||
| } else if (statusCode === 403) { | ||
| msg = "Permission denied. Check your cluster RBAC permissions."; | ||
| } else { | ||
| msg = k8sMessage || "Failed to update job"; | ||
| } | ||
|
|
||
| res.status(statusCode).json({ error: msg }); |
There was a problem hiding this comment.
This if-else if chain can be simplified by using an object to map status codes to their corresponding error messages. This approach is more declarative, readable, and easier to maintain.
const errorMessages = {
404: `Job '${req.params.name}' not found in namespace '${req.params.namespace}'.`,
409: "Conflict: the job was modified by another process. Please refresh and try again.",
403: "Permission denied. Check your cluster RBAC permissions.",
};
const msg = errorMessages[statusCode] || k8sMessage || "Failed to update job";
res.status(statusCode).json({ error: msg });| let msg; | ||
|
|
||
| if (statusCode === 404) { | ||
| msg = `Queue '${req.params.name}' not found.`; | ||
| } else if (statusCode === 409) { | ||
| msg = "Conflict: the queue was modified by another process. Please refresh and try again."; | ||
| } else if (statusCode === 403) { | ||
| msg = "Permission denied. Check your cluster RBAC permissions."; | ||
| } else { | ||
| msg = k8sMessage || "Failed to update queue"; | ||
| } | ||
|
|
||
| res.status(statusCode).json({ error: msg }); |
There was a problem hiding this comment.
Similar to the other error handlers, this if-else if chain can be refactored using an object for mapping status codes to messages. This improves code clarity and maintainability by reducing boilerplate.
const errorMessages = {
404: `Queue '${req.params.name}' not found.`,
409: "Conflict: the queue was modified by another process. Please refresh and try again.",
403: "Permission denied. Check your cluster RBAC permissions.",
};
const msg = errorMessages[statusCode] || k8sMessage || "Failed to update queue";
res.status(statusCode).json({ error: msg });| let msg; | ||
|
|
||
| if (statusCode === 409) { | ||
| const podName = podManifest?.metadata?.name || "unknown"; | ||
| msg = `A pod named '${podName}' already exists in this namespace.`; | ||
| } else if (statusCode === 403) { | ||
| msg = "Permission denied. Check your cluster RBAC permissions."; | ||
| } else if (statusCode === 422) { | ||
| msg = "Invalid pod specification: " + k8sMessage; | ||
| } else { | ||
| msg = k8sMessage || "Failed to create pod"; | ||
| } | ||
| res.status(500).json({ error: msg }); | ||
|
|
||
| res.status(statusCode).json({ error: msg }); |
There was a problem hiding this comment.
This if-else if block can be made more concise and readable by using an object to map status codes to error messages. This also makes it easier to manage error messages.
const errorMessages = {
409: `A pod named '${podManifest?.metadata?.name || "unknown"}' already exists in this namespace.`,
403: "Permission denied. Check your cluster RBAC permissions.",
422: `Invalid pod specification: ${k8sMessage}`,
};
const msg = errorMessages[statusCode] || k8sMessage || "Failed to create pod";
res.status(statusCode).json({ error: msg });| let msg; | ||
|
|
||
| if (statusCode === 409) { | ||
| const jobName = jobManifest?.metadata?.name || "unknown"; | ||
| msg = `A job named '${jobName}' already exists in this namespace.`; | ||
| } else if (statusCode === 403) { | ||
| msg = "Permission denied. Check your cluster RBAC permissions."; | ||
| } else if (statusCode === 422) { | ||
| msg = "Invalid job specification: " + k8sMessage; | ||
| } else { | ||
| msg = k8sMessage || "Failed to create job"; | ||
| } | ||
|
|
||
| res.status(statusCode).json({ error: msg }); |
There was a problem hiding this comment.
To improve readability and maintainability, you can replace this if-else if structure with an object that maps status codes to their respective error messages. This pattern is cleaner and less verbose.
const errorMessages = {
409: `A job named '${jobManifest?.metadata?.name || "unknown"}' already exists in this namespace.`,
403: "Permission denied. Check your cluster RBAC permissions.",
422: `Invalid job specification: ${k8sMessage}`,
};
const msg = errorMessages[statusCode] || k8sMessage || "Failed to create job";
res.status(statusCode).json({ error: msg });| let msg; | ||
|
|
||
| if (statusCode === 409) { | ||
| const queueName = queueManifest?.metadata?.name || "unknown"; | ||
| msg = `A queue named '${queueName}' already exists.`; | ||
| } else if (statusCode === 403) { | ||
| msg = "Permission denied. Check your cluster RBAC permissions."; | ||
| } else if (statusCode === 422) { | ||
| msg = "Invalid queue specification: " + k8sMessage; | ||
| } else { | ||
| msg = k8sMessage || "Failed to create queue"; | ||
| } | ||
|
|
||
| res.status(statusCode).json({ error: msg }); |
There was a problem hiding this comment.
This if-else if chain for handling errors can be refactored into a more concise form using an object to map status codes to messages. This will make the code easier to read and maintain.
const errorMessages = {
409: `A queue named '${queueManifest?.metadata?.name || "unknown"}' already exists.`,
403: "Permission denied. Check your cluster RBAC permissions.",
422: `Invalid queue specification: ${k8sMessage}`,
};
const msg = errorMessages[statusCode] || k8sMessage || "Failed to create queue";
res.status(statusCode).json({ error: msg });
What this PR does
Replaces the generic
500 Internal Server Errorresponses in the backend API with dynamic status codes that capture the actual Kubernetes API errors. Users will now receive specific, actionable error messages (like 409 Conflict, 403 Forbidden, 404 Not Found, and 422 Unprocessable Entity) when a resource creation or update fails, immediately improving troubleshooting in the dashboard.Changes
backend/src/server.js: Updated thecatchblocks forPOSTandPATCHroutes (/api/pods,/api/jobs,/api/queues) to parse the Kubernetes API client error and return the correct HTTP status code and message.