start using "errors"

This commit is contained in:
6543 2022-06-02 15:41:47 +02:00
parent d35e41fa9f
commit d9127d1ed9
2 changed files with 23 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package gitea package gitea
import ( import (
"errors"
"fmt" "fmt"
"net/url" "net/url"
"path" "path"
@ -13,6 +14,8 @@ import (
const giteaAPIRepos = "/api/v1/repos/" const giteaAPIRepos = "/api/v1/repos/"
var ErrorNotFound = errors.New("not found")
type Client struct { type Client struct {
giteaRoot string giteaRoot string
giteaAPIToken string giteaAPIToken string
@ -34,7 +37,6 @@ func NewClient(giteaRoot, giteaAPIToken string) *Client {
} }
// TODOs: // TODOs:
// * own client to store token & giteaRoot
// * handle 404 -> page will show 500 atm // * handle 404 -> page will show 500 atm
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) { func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
@ -47,10 +49,14 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil { if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
return nil, err return nil, err
} }
if res.StatusCode() != fasthttp.StatusOK { switch res.StatusCode() {
case fasthttp.StatusOK:
return res.Body(), nil
case fasthttp.StatusNotFound:
return nil, ErrorNotFound
default:
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode()) return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
} }
return res.Body(), nil
} }
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) { func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
@ -63,7 +69,18 @@ func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
resp := fasthttp.AcquireResponse() resp := fasthttp.AcquireResponse()
resp.SetBodyStream(&strings.Reader{}, -1) resp.SetBodyStream(&strings.Reader{}, -1)
return resp, fastClient.Do(req, resp) if err := fastClient.Do(req, resp); err != nil {
return nil, err
}
switch resp.StatusCode() {
case fasthttp.StatusOK:
return resp, nil
case fasthttp.StatusNotFound:
return nil, ErrorNotFound
default:
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode())
}
} }
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) { func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {

View file

@ -2,6 +2,7 @@ package upstream
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"strings" "strings"
@ -79,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client,
log.Debug().Msg("acquisition") log.Debug().Msg("acquisition")
// Handle errors // Handle errors
if (res == nil && !cachedResponse.Exists) || (res != nil && res.StatusCode() == fasthttp.StatusNotFound) { if (err != nil && errors.Is(err, gitea.ErrorNotFound)) || (res == nil && !cachedResponse.Exists) {
if o.TryIndexPages { if o.TryIndexPages {
// copy the o struct & try if an index page exists // copy the o struct & try if an index page exists
optionsForIndexPages := *o optionsForIndexPages := *o