Skip to content

Fix/4973 json error responses#4977

Open
mehrdadbn9 wants to merge 5 commits intoprometheus:mainfrom
mehrdadbn9:fix/4973-json-error-responses
Open

Fix/4973 json error responses#4977
mehrdadbn9 wants to merge 5 commits intoprometheus:mainfrom
mehrdadbn9:fix/4973-json-error-responses

Conversation

@mehrdadbn9
Copy link

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:

  • Content-Type: application/json
  • Response Body: "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 error field:

  • Content-Type: application/json
  • Response Body: {"error":"bad matcher format: invalidfilter"} (valid JSON object)

Changes Made

  1. OpenAPI Spec (api/v2/openapi.yaml)

    • Changed all error response schemas from type: string to JSON objects with error field
    • Added proper error body types for all error responses (400, 404, 500)
  2. Swagger Code Generation

    • Regenerated REST API code with new error body types
    • Created proper error response structs:
      • GetAlertsBadRequestBody, GetAlertsInternalServerErrorBody
      • GetAlertGroupsBadRequestBody, GetAlertGroupsInternalServerErrorBody
      • GetSilencesBadRequestBody, GetSilencesInternalServerErrorBody
      • GetSilenceInternalServerErrorBody
      • DeleteSilenceInternalServerErrorBody
      • PostAlertsBadRequestBody, PostAlertsInternalServerErrorBody
      • PostSilencesBadRequestBody
  3. API Handlers (api/v2/api.go)

    • Updated all error handlers to create error body objects instead of passing raw strings
    • Fixed the following endpoints:
      • GET /api/v2/alerts
      • POST /api/v2/alerts
      • GET /api/v2/alerts/groups
      • GET /api/v2/silences
      • GET /api/v2/silences/:silence_id
      • DELETE /api/v2/silences/:silence_id
      • POST /api/v2/silences

Example

Before

$ curl -v "http://localhost:9093/api/v2/alerts/groups?filter=invalidfilter"
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
"bad matcher format: invalidfilter"

- 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>
@mehrdadbn9 mehrdadbn9 force-pushed the fix/4973-json-error-responses branch from 5a83340 to 6d140f5 Compare February 8, 2026 12:27
Signed-off-by: mehrdadbn9 <mehdadbiukian@gmail.com>
@mehrdadbn9 mehrdadbn9 force-pushed the fix/4973-json-error-responses branch from 6d140f5 to fde3627 Compare February 8, 2026 12:28
Comment on lines 3 to 16
// 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.
//

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Comment on lines 274 to 277
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
				},
			)

Comment on lines 255 to +274
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mehrdadbn9 mehrdadbn9 force-pushed the fix/4973-json-error-responses branch 3 times, most recently from c357fa8 to f5e6e8f Compare February 9, 2026 02:37
- 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>
@mehrdadbn9 mehrdadbn9 force-pushed the fix/4973-json-error-responses branch from f5e6e8f to 85a72e4 Compare February 9, 2026 02:40
- 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>
@siavashs
Copy link
Contributor

siavashs commented Feb 9, 2026

Reopening the original PR after closing the new one the contributor had opened.
Please continue all updates and discussions here and avoid opening new PRs to fix the exact same issue.

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

  • When feedback arrives about your pull request, respond promptly and professionally even if criticism feels harsh. Maintainers are typically focused on code quality.
  • If changes are requested for your pull request, don't open a new pull request to address the changes. Keeping the existing pull request open and making changes there helps prevent the maintainers from losing context.
  • If your pull request remains unaddressed for weeks, politely follow up with a comment requesting feedback. Do not directly mention the handles of maintainers. Maintainers often balance open source work with full-time responsibilities, and understanding their time constraints fosters better collaboration.
  • If your contribution does not get accepted, ask the maintainers for feedback so that you can have that context for the next time you want to make a contribution.

@siavashs siavashs reopened this Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API endpoint error returns error text with bad content type

2 participants