diff options
author | 2023-11-30 16:22:34 +0000 | |
---|---|---|
committer | 2023-11-30 16:22:34 +0000 | |
commit | eb170003b81504ba6eb85f950c223dc9eaf1cfca (patch) | |
tree | f1f9779e14875faa70f4db85a8cf19100633884d /internal/gtserror | |
parent | [bugfix] always go through status parent dereferencing on isNew, even on data... (diff) | |
download | gotosocial-eb170003b81504ba6eb85f950c223dc9eaf1cfca.tar.xz |
[bugfix] return 400 Bad Request on more cases of malformed AS data (#2399)
Diffstat (limited to 'internal/gtserror')
-rw-r--r-- | internal/gtserror/error.go | 59 | ||||
-rw-r--r-- | internal/gtserror/new_caller.go | 2 | ||||
-rw-r--r-- | internal/gtserror/withcode.go | 5 |
3 files changed, 39 insertions, 27 deletions
diff --git a/internal/gtserror/error.go b/internal/gtserror/error.go index 21d580c4e..8338d30a4 100644 --- a/internal/gtserror/error.go +++ b/internal/gtserror/error.go @@ -35,39 +35,36 @@ const ( errorTypeKey unrtrvableKey wrongTypeKey - - // Types returnable from Type(...). - TypeSMTP ErrorType = "smtp" // smtp (mail) + smtpKey + malformedKey ) -// Unretrievable checks error for a stored "unretrievable" flag. -// -// Unretrievable indicates that a call to retrieve a resource +// IsUnretrievable indicates that a call to retrieve a resource // (account, status, attachment, etc) could not be fulfilled, // either because it was not found locally, or because some // prerequisite remote resource call failed, making it impossible // to return the item. -func Unretrievable(err error) bool { +func IsUnretrievable(err error) bool { _, ok := errors.Value(err, unrtrvableKey).(struct{}) return ok } // SetUnretrievable will wrap the given error to store an "unretrievable" -// flag, returning wrapped error. See "Unretrievable" for example use-cases. +// flag, returning wrapped error. See Unretrievable() for example use-cases. func SetUnretrievable(err error) error { return errors.WithValue(err, unrtrvableKey, struct{}{}) } -// WrongType checks error for a stored "wrong type" flag. Wrong type +// IsWrongType checks error for a stored "wrong type" flag. Wrong type // indicates that an ActivityPub URI returned a type we weren't expecting: // Statusable instead of Accountable, or vice versa, for example. -func WrongType(err error) bool { +func IsWrongType(err error) bool { _, ok := errors.Value(err, wrongTypeKey).(struct{}) return ok } // SetWrongType will wrap the given error to store a "wrong type" flag, -// returning wrapped error. See "WrongType" for example use-cases. +// returning wrapped error. See IsWrongType() for example use-cases. func SetWrongType(err error) error { return errors.WithValue(err, wrongTypeKey, struct{}{}) } @@ -86,29 +83,41 @@ func WithStatusCode(err error, code int) error { return errors.WithValue(err, statusCodeKey, code) } -// NotFound checks error for a stored "not found" flag. For example -// an error from an outgoing HTTP request due to DNS lookup. -func NotFound(err error) bool { +// IsNotFound checks error for a stored "not found" flag. For +// example an error from an outgoing HTTP request due to DNS lookup. +func IsNotFound(err error) bool { _, ok := errors.Value(err, notFoundKey).(struct{}) return ok } // SetNotFound will wrap the given error to store a "not found" flag, -// returning wrapped error. See NotFound() for example use-cases. +// returning wrapped error. See IsNotFound() for example use-cases. func SetNotFound(err error) error { return errors.WithValue(err, notFoundKey, struct{}{}) } -// Type checks error for a stored "type" value. For example -// an error from sending an email may set a value of "smtp" -// to indicate this was an SMTP error. -func Type(err error) ErrorType { - s, _ := errors.Value(err, errorTypeKey).(ErrorType) - return s +// IsSMTP checks error for a stored "smtp" flag. For +// example an error from outgoing SMTP email attempt. +func IsSMTP(err error) bool { + _, ok := errors.Value(err, smtpKey).(struct{}) + return ok +} + +// SetSMTP will wrap the given error to store an "smtp" flag, +// returning wrapped error. See IsSMTP() for example use-cases. +func SetSMTP(err error) error { + return errors.WithValue(err, smtpKey, struct{}{}) +} + +// IsMalformed checks error for a stored "malformed" flag. For +// example an error from an incoming ActivityStreams type conversion. +func IsMalformed(err error) bool { + _, ok := errors.Value(err, malformedKey).(struct{}) + return ok } -// SetType will wrap the given error to store a "type" value, -// returning wrapped error. See Type() for example use-cases. -func SetType(err error, errType ErrorType) error { - return errors.WithValue(err, errorTypeKey, errType) +// SetMalformed will wrap the given error to store a "malformed" flag, +// returning wrapped error. See IsMalformed() for example use-cases. +func SetMalformed(err error) error { + return errors.WithValue(err, malformedKey, struct{}{}) } diff --git a/internal/gtserror/new_caller.go b/internal/gtserror/new_caller.go index 46ae76d6a..f7e0c84f6 100644 --- a/internal/gtserror/new_caller.go +++ b/internal/gtserror/new_caller.go @@ -61,7 +61,7 @@ func newfAt(calldepth int, msgf string, args ...any) error { } } -// caller fetches the calling function name, skipping 'depth'. Results are cached per PC. +// caller fetches the calling function name, skipping 'depth'. func caller(depth int) string { var pcs [1]uintptr diff --git a/internal/gtserror/withcode.go b/internal/gtserror/withcode.go index d17a4e42e..da489225c 100644 --- a/internal/gtserror/withcode.go +++ b/internal/gtserror/withcode.go @@ -39,13 +39,16 @@ type WithCode interface { // Unwrap returns the original error. // This should *NEVER* be returned to a client as it may contain sensitive information. Unwrap() error + // Error serializes the original internal error for debugging within the GoToSocial logs. // This should *NEVER* be returned to a client as it may contain sensitive information. Error() string + // Safe returns the API-safe version of the error for serialization towards a client. // There's not much point logging this internally because it won't contain much helpful information. Safe() string - // Code returns the status code for serving to a client. + + // Code returns the status code for serving to a client. Code() int } |