improve & refactor & return specific error pages

This commit is contained in:
6543 2022-11-07 22:47:03 +01:00
parent 60aefb4bf5
commit 70871e77be
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
4 changed files with 45 additions and 24 deletions

View file

@ -2,6 +2,7 @@ package upstream
import (
"errors"
"fmt"
"io"
"net/http"
"strings"
@ -56,23 +57,25 @@ type Options struct {
func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (final bool) {
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
if o.TargetOwner == "" || o.TargetRepo == "" {
html.ReturnErrorPage(ctx, "either repo owner or name info is missing", http.StatusBadRequest)
return true
}
// Check if the branch exists and when it was modified
if o.BranchTimestamp.IsZero() {
branch := GetBranchTimestamp(giteaClient, o.TargetOwner, o.TargetRepo, o.TargetBranch)
if branch == nil {
html.ReturnErrorPage(ctx, "", http.StatusFailedDependency)
if branch == nil || branch.Branch == "" {
html.ReturnErrorPage(ctx,
fmt.Sprintf("could not get timestamp of branch '%s'", o.TargetBranch),
http.StatusFailedDependency)
return true
}
o.TargetBranch = branch.Branch
o.BranchTimestamp = branch.Timestamp
}
if o.TargetOwner == "" || o.TargetRepo == "" || o.TargetBranch == "" {
html.ReturnErrorPage(ctx, "", http.StatusBadRequest)
return true
}
// Check if the browser has a cached version
if ifModifiedSince, err := time.Parse(time.RFC1123, string(ctx.Response().Header.Get(headerIfModifiedSince))); err == nil {
if !ifModifiedSince.Before(o.BranchTimestamp) {
@ -127,7 +130,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
}
if res != nil && (err != nil || res.StatusCode != http.StatusOK) {
log.Printf("Couldn't fetch contents (status code %d): %v\n", res.StatusCode, err)
html.ReturnErrorPage(ctx, "", http.StatusInternalServerError)
html.ReturnErrorPage(ctx, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return true
}