Skip to content

Conversation

@thedae
Copy link
Member

@thedae thedae commented May 20, 2025

Added a way to write response headers on error

Usage

If the triggered error implements a Headers() map[string][]string, the returned map will be written as response headers. For example, when implementing a quota system, you could return a 429 with a Retry-After response header like this:

type errProxyQuotaExceeded struct {
	error
	headers map[string][]string
}

func NewErrProxyQuotaExceeded(blockedFor string) error {
	err := errProxyQuotaExceeded{error: errors.New("quota exceeded")}

	err.headers = map[string][]string{
		'Retry-After': {blockedFor},
	}
	return err
}

func (errProxyQuotaExceeded) StatusCode() int {
	return http.StatusTooManyRequests
}

func (e errProxyQuotaExceeded) Headers() map[string][]string {
	return e.headers
}

Signed-off-by: thedae <thedae@gmail.com>
@thedae thedae requested review from dhontecillas and kpacha May 20, 2025 08:57
thedae added 2 commits May 20, 2025 11:02
Signed-off-by: thedae <thedae@gmail.com>
Signed-off-by: thedae <thedae@gmail.com>
@kpacha kpacha merged commit aa37e1e into master May 22, 2025
5 checks passed
@kpacha kpacha deleted the error_response_headers branch May 22, 2025 16:31
@github-actions
Copy link

This pull request was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants