mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
error: allow storing extra headers in RespError
This commit is contained in:
parent
8b04430d84
commit
b041eb924e
1 changed files with 17 additions and 1 deletions
18
error.go
18
error.go
|
|
@ -140,7 +140,8 @@ type RespError struct {
|
|||
Err string
|
||||
ExtraData map[string]any
|
||||
|
||||
StatusCode int
|
||||
StatusCode int
|
||||
ExtraHeader map[string]string
|
||||
}
|
||||
|
||||
func (e *RespError) UnmarshalJSON(data []byte) error {
|
||||
|
|
@ -168,6 +169,9 @@ func (e RespError) Write(w http.ResponseWriter) {
|
|||
if statusCode == 0 {
|
||||
statusCode = http.StatusInternalServerError
|
||||
}
|
||||
for key, value := range e.ExtraHeader {
|
||||
w.Header().Set(key, value)
|
||||
}
|
||||
exhttp.WriteJSONResponse(w, statusCode, &e)
|
||||
}
|
||||
|
||||
|
|
@ -190,6 +194,18 @@ func (e RespError) WithExtraData(extraData map[string]any) RespError {
|
|||
return e
|
||||
}
|
||||
|
||||
func (e RespError) WithExtraHeader(key, value string) RespError {
|
||||
e.ExtraHeader = exmaps.NonNilClone(e.ExtraHeader)
|
||||
e.ExtraHeader[key] = value
|
||||
return e
|
||||
}
|
||||
|
||||
func (e RespError) WithExtraHeaders(headers map[string]string) RespError {
|
||||
e.ExtraHeader = exmaps.NonNilClone(e.ExtraHeader)
|
||||
maps.Copy(e.ExtraHeader, headers)
|
||||
return e
|
||||
}
|
||||
|
||||
// Error returns the errcode and error message.
|
||||
func (e RespError) Error() string {
|
||||
return e.ErrCode + ": " + e.Err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue