diff options
author | John Olheiser <42128690+jolheiser@users.noreply.github.com> | 2019-03-18 21:29:43 -0500 |
---|---|---|
committer | techknowlogick <matti@mdranta.net> | 2019-03-18 22:29:43 -0400 |
commit | cac9e6e7605184f5679b1ebfbe5b5805191d9a53 (patch) | |
tree | 459a00974c8eff4f9cad8843b16d2a1f476c5912 /modules/context | |
parent | d10a668ffc4ed2a81a8a62ee78f0885ede713ddd (diff) | |
download | gitea-cac9e6e7605184f5679b1ebfbe5b5805191d9a53.tar.gz gitea-cac9e6e7605184f5679b1ebfbe5b5805191d9a53.zip |
Updates to API 404 responses (#6077)
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/api.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index b27ffcbc8c..7ec4d9036c 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -1,4 +1,5 @@ // Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -6,6 +7,8 @@ package context import ( "fmt" + "net/url" + "path" "strings" "github.com/go-macaron/csrf" @@ -140,3 +143,29 @@ func ReferencesGitRepo() macaron.Handler { } } } + +// NotFound handles 404s for APIContext +// String will replace message, errors will be added to a slice +func (ctx *APIContext) NotFound(objs ...interface{}) { + var message = "Not Found" + var errors []string + for _, obj := range objs { + if err, ok := obj.(error); ok { + errors = append(errors, err.Error()) + } else { + message = obj.(string) + } + } + + u, err := url.Parse(setting.AppURL) + if err != nil { + ctx.Error(500, "Invalid AppURL", err) + return + } + u.Path = path.Join(u.Path, "api", "swagger") + ctx.JSON(404, map[string]interface{}{ + "message": message, + "documentation_url": u.String(), + "errors": errors, + }) +} |