mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
bridgev2/provisioning: include internal error in separate response field
This commit is contained in:
parent
974f7dc544
commit
265e26046a
2 changed files with 22 additions and 2 deletions
|
|
@ -604,7 +604,7 @@ func RespondWithError(w http.ResponseWriter, err error, message string) {
|
||||||
if errors.As(err, &we) {
|
if errors.As(err, &we) {
|
||||||
we.Write(w)
|
we.Write(w)
|
||||||
} else {
|
} else {
|
||||||
mautrix.MUnknown.WithMessage(message).Write(w)
|
mautrix.MUnknown.WithMessage(message).WithInternalError(err).Write(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
22
error.go
22
error.go
|
|
@ -143,7 +143,8 @@ type RespError struct {
|
||||||
StatusCode int
|
StatusCode int
|
||||||
ExtraHeader map[string]string
|
ExtraHeader map[string]string
|
||||||
|
|
||||||
CanRetry bool
|
CanRetry bool
|
||||||
|
InternalError string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *RespError) UnmarshalJSON(data []byte) error {
|
func (e *RespError) UnmarshalJSON(data []byte) error {
|
||||||
|
|
@ -154,6 +155,7 @@ func (e *RespError) UnmarshalJSON(data []byte) error {
|
||||||
e.ErrCode, _ = e.ExtraData["errcode"].(string)
|
e.ErrCode, _ = e.ExtraData["errcode"].(string)
|
||||||
e.Err, _ = e.ExtraData["error"].(string)
|
e.Err, _ = e.ExtraData["error"].(string)
|
||||||
e.CanRetry, _ = e.ExtraData["com.beeper.can_retry"].(bool)
|
e.CanRetry, _ = e.ExtraData["com.beeper.can_retry"].(bool)
|
||||||
|
e.InternalError, _ = e.ExtraData["fi.mau.internal_error"].(string)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,6 +165,13 @@ func (e *RespError) MarshalJSON() ([]byte, error) {
|
||||||
data["error"] = e.Err
|
data["error"] = e.Err
|
||||||
if e.CanRetry {
|
if e.CanRetry {
|
||||||
data["com.beeper.can_retry"] = e.CanRetry
|
data["com.beeper.can_retry"] = e.CanRetry
|
||||||
|
} else {
|
||||||
|
delete(data, "com.beeper.can_retry")
|
||||||
|
}
|
||||||
|
if e.InternalError != "" {
|
||||||
|
data["fi.mau.internal_error"] = e.InternalError
|
||||||
|
} else {
|
||||||
|
delete(data, "fi.mau.internal_error")
|
||||||
}
|
}
|
||||||
return json.Marshal(data)
|
return json.Marshal(data)
|
||||||
}
|
}
|
||||||
|
|
@ -199,12 +208,23 @@ func (e RespError) WithCanRetry(canRetry bool) RespError {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e RespError) WithInternalError(err error) RespError {
|
||||||
|
e.InternalError = err.Error()
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
func (e RespError) WithExtraData(extraData map[string]any) RespError {
|
func (e RespError) WithExtraData(extraData map[string]any) RespError {
|
||||||
e.ExtraData = exmaps.NonNilClone(e.ExtraData)
|
e.ExtraData = exmaps.NonNilClone(e.ExtraData)
|
||||||
maps.Copy(e.ExtraData, extraData)
|
maps.Copy(e.ExtraData, extraData)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e RespError) WithExtraField(key string, value any) RespError {
|
||||||
|
e.ExtraData = exmaps.NonNilClone(e.ExtraData)
|
||||||
|
e.ExtraData[key] = value
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
func (e RespError) WithExtraHeader(key, value string) RespError {
|
func (e RespError) WithExtraHeader(key, value string) RespError {
|
||||||
e.ExtraHeader = exmaps.NonNilClone(e.ExtraHeader)
|
e.ExtraHeader = exmaps.NonNilClone(e.ExtraHeader)
|
||||||
e.ExtraHeader[key] = value
|
e.ExtraHeader[key] = value
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue