[bugfix] internal server error on search not found (#1590)

* add error value wrapping, include status code / not found flags from transport errors, update error usages

Signed-off-by: kim <grufwub@gmail.com>

* add code commenting for gtserror functions

Signed-off-by: kim <grufwub@gmail.com>

---------

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim
2023-03-06 09:38:43 +00:00
committed by GitHub
parent f518f649f8
commit d8d5818b47
9 changed files with 108 additions and 37 deletions

View File

@@ -20,7 +20,6 @@ package transport
import (
"context"
"errors"
"fmt"
"io"
"net/http"
@@ -28,15 +27,10 @@ import (
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/uris"
)
// ErrGone is returned from Dereference when the remote resource returns 410 GONE.
// This is useful in cases where we're processing a delete of a resource that's already
// been removed from the remote server, so we know we don't need to keep trying to
// dereference it.
var ErrGone = errors.New("remote resource returned HTTP code 410 GONE")
func (t *transport) Dereference(ctx context.Context, iri *url.URL) ([]byte, error) {
// if the request is to us, we can shortcut for certain URIs rather than going through
// the normal request flow, thereby saving time and energy
@@ -72,12 +66,10 @@ func (t *transport) Dereference(ctx context.Context, iri *url.URL) ([]byte, erro
}
defer rsp.Body.Close()
switch rsp.StatusCode {
case http.StatusOK:
return io.ReadAll(rsp.Body)
case http.StatusGone:
return nil, ErrGone
default:
return nil, fmt.Errorf("GET request to %s failed: %s", iriStr, rsp.Status)
if rsp.StatusCode != http.StatusOK {
err := fmt.Errorf("GET request to %s failed: %s", iriStr, rsp.Status)
return nil, gtserror.WithStatusCode(err, rsp.StatusCode)
}
return io.ReadAll(rsp.Body)
}