[bugfix] Rework MultiError to wrap + unwrap errors properly (#2057)

* rework multierror a bit

* test multierror
This commit is contained in:
tobi
2023-08-02 17:21:46 +02:00
committed by GitHub
parent 2cee8f2dd8
commit e8a20f587c
24 changed files with 263 additions and 154 deletions

View File

@@ -22,7 +22,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
@@ -105,16 +104,16 @@ func (suite *InboxPostTestSuite) inboxPost(
suite.FailNow(err.Error())
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {

View File

@@ -90,16 +90,16 @@ func (suite *AccountUpdateTestSuite) updateAccount(
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {

View File

@@ -19,7 +19,6 @@ package accounts_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
@@ -63,16 +62,16 @@ func (suite *ListsTestSuite) getLists(targetAccountID string, expectedHTTPStatus
suite.FailNow(err.Error())
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {

View File

@@ -19,7 +19,6 @@ package accounts_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
@@ -99,16 +98,16 @@ func (suite *AccountSearchTestSuite) getSearch(
suite.FailNow(err.Error())
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {

View File

@@ -19,7 +19,6 @@ package admin_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -84,16 +83,16 @@ func (suite *ReportResolveTestSuite) resolveReport(
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
if expectedBody != "" {
if string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
return nil, errs.Combine()
}

View File

@@ -19,7 +19,6 @@ package admin_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -101,16 +100,16 @@ func (suite *ReportsGetTestSuite) getReports(
return nil, "", err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
if expectedBody != "" {
if string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
return nil, "", errs.Combine()
}

View File

@@ -19,7 +19,6 @@ package lists_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -103,17 +102,17 @@ func (suite *ListAccountsTestSuite) getListAccounts(
return nil, "", err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
if expectedBody != "" {
if string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
return nil, "", errs.Combine()
}

View File

@@ -19,7 +19,6 @@ package reports_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -77,17 +76,17 @@ func (suite *ReportCreateTestSuite) createReport(expectedHTTPStatus int, expecte
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
if expectedBody != "" {
if string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
return nil, errs.Combine()
}

View File

@@ -19,7 +19,6 @@ package reports_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -64,17 +63,17 @@ func (suite *ReportGetTestSuite) getReport(expectedHTTPStatus int, expectedBody
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// check code + body
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
if expectedBody != "" {
if string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
return nil, errs.Combine()
}

View File

@@ -22,7 +22,6 @@ import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
@@ -122,16 +121,16 @@ func (suite *SearchGetTestSuite) getSearch(
suite.FailNow(err.Error())
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d: %v", expectedHTTPStatus, resultCode, ctx.Errors.JSON()))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {

View File

@@ -20,7 +20,6 @@ package statuses_test
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -74,20 +73,20 @@ func (suite *StatusPinTestSuite) createPin(
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// check code + body
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if len(errs) > 0 {
return nil, errs.Combine()
if err := errs.Combine(); err != nil {
suite.FailNow("", "%v (body %s)", err, string(b))
}
resp := &apimodel.Status{}

View File

@@ -19,7 +19,6 @@ package statuses_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -68,20 +67,20 @@ func (suite *StatusUnpinTestSuite) createUnpin(
return nil, err
}
errs := gtserror.MultiError{}
errs := gtserror.NewMultiError(2)
// check code + body
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// if we got an expected body, return early
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b)))
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if len(errs) > 0 {
return nil, errs.Combine()
if err := errs.Combine(); err != nil {
suite.FailNow("", "%v (body %s)", err, string(b))
}
resp := &apimodel.Status{}