| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package mautrix |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"net/http" |
| 14 |
) |
| 15 |
|
| 16 |
// Common error codes from https://matrix.org/docs/spec/client_server/latest#api-standards |
| 17 |
// |
| 18 |
// Can be used with errors.Is() to check the response code without casting the error: |
| 19 |
// err := client.Sync() |
| 20 |
// if errors.Is(err, MUnknownToken) { |
| 21 |
// // logout |
| 22 |
// } |
| 23 |
var ( |
| 24 |
// Forbidden access, e.g. joining a room without permission, failed login. |
| 25 |
MForbidden = RespError{ErrCode: "M_FORBIDDEN"} |
| 26 |
// The access token specified was not recognised. |
| 27 |
MUnknownToken = RespError{ErrCode: "M_UNKNOWN_TOKEN"} |
| 28 |
// No access token was specified for the request. |
| 29 |
MMissingToken = RespError{ErrCode: "M_MISSING_TOKEN"} |
| 30 |
// Request contained valid JSON, but it was malformed in some way, e.g. missing required keys, invalid values for keys. |
| 31 |
MBadJSON = RespError{ErrCode: "M_BAD_JSON"} |
| 32 |
// Request did not contain valid JSON. |
| 33 |
MNotJSON = RespError{ErrCode: "M_NOT_JSON"} |
| 34 |
// No resource was found for this request. |
| 35 |
MNotFound = RespError{ErrCode: "M_NOT_FOUND"} |
| 36 |
// Too many requests have been sent in a short period of time. Wait a while then try again. |
| 37 |
MLimitExceeded = RespError{ErrCode: "M_LIMIT_EXCEEDED"} |
| 38 |
// The user ID associated with the request has been deactivated. |
| 39 |
// Typically for endpoints that prove authentication, such as /login. |
| 40 |
MUserDeactivated = RespError{ErrCode: "M_USER_DEACTIVATED"} |
| 41 |
// Encountered when trying to register a user ID which has been taken. |
| 42 |
MUserInUse = RespError{ErrCode: "M_USER_IN_USE"} |
| 43 |
// Encountered when trying to register a user ID which is not valid. |
| 44 |
MInvalidUsername = RespError{ErrCode: "M_INVALID_USERNAME"} |
| 45 |
// Sent when the room alias given to the createRoom API is already in use. |
| 46 |
MRoomInUse = RespError{ErrCode: "M_ROOM_IN_USE"} |
| 47 |
// The state change requested cannot be performed, such as attempting to unban a user who is not banned. |
| 48 |
MBadState = RespError{ErrCode: "M_BAD_STATE"} |
| 49 |
// The request or entity was too large. |
| 50 |
MTooLarge = RespError{ErrCode: "M_TOO_LARGE"} |
| 51 |
// The resource being requested is reserved by an application service, or the application service making the request has not created the resource. |
| 52 |
MExclusive = RespError{ErrCode: "M_EXCLUSIVE"} |
| 53 |
// The client's request to create a room used a room version that the server does not support. |
| 54 |
MUnsupportedRoomVersion = RespError{ErrCode: "M_UNSUPPORTED_ROOM_VERSION"} |
| 55 |
// The client attempted to join a room that has a version the server does not support. |
| 56 |
// Inspect the room_version property of the error response for the room's version. |
| 57 |
MIncompatibleRoomVersion = RespError{ErrCode: "M_INCOMPATIBLE_ROOM_VERSION"} |
| 58 |
) |
| 59 |
|
| 60 |
// HTTPError An HTTP Error response, which may wrap an underlying native Go Error. |
| 61 |
type HTTPError struct { |
| 62 |
Request *http.Request |
| 63 |
Response *http.Response |
| 64 |
ResponseBody string |
| 65 |
|
| 66 |
WrappedError error |
| 67 |
RespError *RespError |
| 68 |
Message string |
| 69 |
} |
| 70 |
|
| 71 |
func (e HTTPError) Is(err error) bool { |
| 72 |
return (e.RespError != nil && errors.Is(e.RespError, err)) || (e.WrappedError != nil && errors.Is(e.WrappedError, err)) |
| 73 |
} |
| 74 |
|
| 75 |
func (e HTTPError) IsStatus(code int) bool { |
| 76 |
return e.Response != nil && e.Response.StatusCode == code |
| 77 |
} |
| 78 |
|
| 79 |
func (e HTTPError) Error() string { |
| 80 |
if e.WrappedError != nil { |
| 81 |
return fmt.Sprintf("%s: %v", e.Message, e.WrappedError) |
| 82 |
} else if e.RespError != nil { |
| 83 |
return fmt.Sprintf("failed to %s %s: %s (HTTP %d): %s", e.Request.Method, e.Request.URL.Path, |
| 84 |
e.RespError.ErrCode, e.Response.StatusCode, e.RespError.Err) |
| 85 |
} else { |
| 86 |
msg := fmt.Sprintf("failed to %s %s: %s", e.Request.Method, e.Request.URL.Path, e.Response.Status) |
| 87 |
if len(e.ResponseBody) > 0 { |
| 88 |
msg = fmt.Sprintf("%s\n%s", msg, e.ResponseBody) |
| 89 |
} |
| 90 |
return msg |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func (e HTTPError) Unwrap() error { |
| 95 |
if e.WrappedError != nil { |
| 96 |
return e.WrappedError |
| 97 |
} else if e.RespError != nil { |
| 98 |
return *e.RespError |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
// RespError is the standard JSON error response from Homeservers. It also implements the Golang "error" interface. |
| 104 |
// See https://spec.matrix.org/v1.2/client-server-api/#api-standards |
| 105 |
type RespError struct { |
| 106 |
ErrCode string |
| 107 |
Err string |
| 108 |
ExtraData map[string]interface{} |
| 109 |
} |
| 110 |
|
| 111 |
func (e *RespError) UnmarshalJSON(data []byte) error { |
| 112 |
err := json.Unmarshal(data, &e.ExtraData) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
e.ErrCode, _ = e.ExtraData["errcode"].(string) |
| 117 |
e.Err, _ = e.ExtraData["error"].(string) |
| 118 |
return nil |
| 119 |
} |
| 120 |
|
| 121 |
func (e *RespError) MarshalJSON() ([]byte, error) { |
| 122 |
if e.ExtraData == nil { |
| 123 |
e.ExtraData = make(map[string]interface{}) |
| 124 |
} |
| 125 |
e.ExtraData["errcode"] = e.ErrCode |
| 126 |
e.ExtraData["error"] = e.Err |
| 127 |
return json.Marshal(&e.ExtraData) |
| 128 |
} |
| 129 |
|
| 130 |
// Error returns the errcode and error message. |
| 131 |
func (e RespError) Error() string { |
| 132 |
return e.ErrCode + ": " + e.Err |
| 133 |
} |
| 134 |
|
| 135 |
func (e RespError) Is(err error) bool { |
| 136 |
e2, ok := err.(RespError) |
| 137 |
if !ok { |
| 138 |
return false |
| 139 |
} |
| 140 |
if e.ErrCode == "M_UNKNOWN" && e2.ErrCode == "M_UNKNOWN" { |
| 141 |
return e.Err == e2.Err |
| 142 |
} |
| 143 |
return e2.ErrCode == e.ErrCode |
| 144 |
} |