Conversation
- Updated OpenAPI spec to use JSON error response objects with 'error' field
- Regenerated swagger code with new error body types
- Updated API handlers to use JSON error responses instead of plain text strings
- All error responses now return proper JSON: {"error": "message"}
- Fixes issue where error responses were plain text with incorrect Content-Type
Signed-off-by: mehrdadbn9 <mehdadbiukian@gmail.com>
5a83340 to
6d140f5
Compare
Signed-off-by: mehrdadbn9 <mehdadbiukian@gmail.com>
6d140f5 to
fde3627
Compare
| // Copyright Prometheus Team | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
There was a problem hiding this comment.
Code generation command was not run correctly, hence the copyright header is removed.
You must only use the provided make target to regenerate code.
(This issue has happened in all files, I'm only pointing out one as an example!)
api/v2/api.go
Outdated
| logger.Debug("Failed to compile receiver regex", "err", err) | ||
| return alert_ops. | ||
| NewGetAlertsBadRequest(). | ||
| WithPayload( | ||
| fmt.Sprintf("failed to parse receiver param: %v", err.Error()), | ||
| ) | ||
| errorMsg := fmt.Sprintf("failed to parse receiver param: %v", err.Error()) | ||
| body := &alert_ops.GetAlertsBadRequestBody{Error: &errorMsg} | ||
| return alert_ops.NewGetAlertsBadRequest().WithPayload(body) |
There was a problem hiding this comment.
Let's use the same message in the logs and the api, for example:
message := "Failed to compile receiver regex"
logger.Debug(message, "err", err)
errMsg := fmt.Sprintf("%s: %v", message, err)
return alert_ops.NewGetAlertsBadRequest().WithPayload(
&alert_ops.GetAlertsBadRequestBody{
Error: &errMsg,
},
)| BadRequest: | ||
| description: Bad request | ||
| schema: | ||
| type: string | ||
| type: object | ||
| properties: | ||
| error: | ||
| type: string | ||
| required: | ||
| - error | ||
| InternalServerError: | ||
| description: Internal server error | ||
| schema: | ||
| type: string | ||
| type: object | ||
| properties: | ||
| error: | ||
| type: string | ||
| required: | ||
| - error | ||
| NotFound: | ||
| description: Resource not found |
There was a problem hiding this comment.
While this is a fix, I'm hesitant to break the API as it can cause problems for custom tools or automations that people use to integrate with Alertmanager API.
c357fa8 to
f5e6e8f
Compare
- Regenerated swagger code with proper copyright headers using --copyright-file
- Added message variable pattern to ensure error messages are defined once and used consistently in both logs and API responses
- All error responses now return proper JSON: {"error": "message"}
- Fixes issue where error responses were plain text with incorrect Content-Type
Signed-off-by: mehrdadbn9 <mehrdadbiukian@gmail.com>
f5e6e8f to
85a72e4
Compare
- Fixed missing message variable in getAlertGroupsHandler error handler - Fixed missing message variable in getSilencesHandler error handler - Fixed incorrect response type in getSilencesHandler (was using GetAlertGroupsBadRequest) - Added missing error body to getAlertGroupsHandler internal server error - Fixed error message formatting in postAlertsHandler to use message variable pattern - Fixed error message formatting in postAlertsHandler validation errors All error handlers now consistently use the message variable pattern and return proper JSON error responses. Signed-off-by: mehrdadbn9 <mehrdadbiukian@gmail.com>
|
Reopening the original PR after closing the new one the contributor had opened. Please see this guide on contributing to projects: https://docs.github.com/en/get-started/exploring-projects-on-github/contributing-to-open-source#working-with-project-maintainers
|
Fix #4973: Return JSON error responses instead of plain text
Problem
The Alertmanager API was returning error responses as plain text strings with incorrect Content-Type header:
application/json"bad matcher format: invalidfilter"(plain text string)This violates the JSON API specification as the response body is a quoted string, not a JSON object.
Solution
Updated the API to return proper JSON error responses with an
errorfield:application/json{"error":"bad matcher format: invalidfilter"}(valid JSON object)Changes Made
OpenAPI Spec (
api/v2/openapi.yaml)type: stringto JSON objects witherrorfieldSwagger Code Generation
GetAlertsBadRequestBody,GetAlertsInternalServerErrorBodyGetAlertGroupsBadRequestBody,GetAlertGroupsInternalServerErrorBodyGetSilencesBadRequestBody,GetSilencesInternalServerErrorBodyGetSilenceInternalServerErrorBodyDeleteSilenceInternalServerErrorBodyPostAlertsBadRequestBody,PostAlertsInternalServerErrorBodyPostSilencesBadRequestBodyAPI Handlers (
api/v2/api.go)Example
Before