From cac9e6e7605184f5679b1ebfbe5b5805191d9a53 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Mon, 18 Mar 2019 21:29:43 -0500 Subject: Updates to API 404 responses (#6077) --- modules/context/api.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'modules') 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, + }) +} -- cgit v1.2.3