Handle network errors better when returning remote HTTP errors

This commit is contained in:
Kegan Dougal 2020-06-25 13:29:00 +01:00
parent f3d9bf0670
commit 3cc40408fc
3 changed files with 6 additions and 6 deletions

View file

@ -90,7 +90,6 @@ func (r *FederationSenderInternalAPI) PerformJoin(
// If we reach here then we didn't complete a join for some reason. // If we reach here then we didn't complete a join for some reason.
var httpErr gomatrix.HTTPError var httpErr gomatrix.HTTPError
if ok := errors.As(lastErr, &httpErr); ok { if ok := errors.As(lastErr, &httpErr); ok {
logrus.Infof("TYPE CAST LASTERR OK!")
httpErr.Message = string(httpErr.Contents) httpErr.Message = string(httpErr.Contents)
response.LastError = &httpErr response.LastError = &httpErr
} else { } else {

View file

@ -87,7 +87,7 @@ func (h *httpFederationSenderInternalAPI) PerformJoin(
if err != nil { if err != nil {
response.LastError = &gomatrix.HTTPError{ response.LastError = &gomatrix.HTTPError{
Message: err.Error(), Message: err.Error(),
Code: 599, // to distinguish from genuine 500 Code: 0,
WrappedError: err, WrappedError: err,
} }
} }

View file

@ -46,12 +46,13 @@ func (p *PerformError) JSONResponse() util.JSONResponse {
JSON: jsonerror.Forbidden(p.Msg), JSON: jsonerror.Forbidden(p.Msg),
} }
case PerformErrRemote: case PerformErrRemote:
code := p.RemoteCode // if the code is 0 then something bad happened and it isn't
if code == 0 { // a remote HTTP error being encapsulated, e.g network error to remote.
code = 500 if p.RemoteCode == 0 {
return util.ErrorResponse(fmt.Errorf("%s", p.Msg))
} }
return util.JSONResponse{ return util.JSONResponse{
Code: code, Code: p.RemoteCode,
// TODO: Should we assert this is in fact JSON? E.g gjson parse? // TODO: Should we assert this is in fact JSON? E.g gjson parse?
JSON: json.RawMessage(p.Msg), JSON: json.RawMessage(p.Msg),
} }