1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-04 02:37:14 +00:00

HandleHTTPError MVC Method as requested at #1595. Read HISTORY.md

example at: https://github.com/kataras/iris/tree/master/_examples/mvc/error-handler-http
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-22 08:04:22 +03:00
parent a018ba9b0a
commit 8e049d77c9
15 changed files with 186 additions and 16 deletions

View File

@@ -370,7 +370,7 @@ func (api *APIBuilder) SetRegisterRule(rule RouteRegisterRule) Party {
return api
}
// Handle registers a route to the server's api.
// Handle registers a route to this Party.
// if empty method is passed then handler(s) are being registered to all methods, same as .Any.
//
// Returns a *Route, app will throw any errors later on.
@@ -378,6 +378,8 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
return api.handle(0, method, relativePath, handlers...)
}
// handle registers a full route to this Party.
// Use Handle or Get, Post, Put, Delete and et.c. instead.
func (api *APIBuilder) handle(errorCode int, method string, relativePath string, handlers ...context.Handler) *Route {
routes := api.createRoutes(errorCode, []string{method}, relativePath, handlers...)
@@ -1242,6 +1244,14 @@ func (api *APIBuilder) OnAnyErrorCode(handlers ...context.Handler) (routes []*Ro
routes = append(routes, api.OnErrorCode(statusCode, handlers...)...)
}
if n := len(routes); n > 1 {
for _, r := range routes[1:n] {
r.NoLog = true
}
routes[0].Title = "ERR"
}
return
}

View File

@@ -189,7 +189,13 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
return lsub1 > lsub2
})
noLogCount := 0
for _, r := range registeredRoutes {
if r.NoLog {
noLogCount++
}
if h.config != nil && h.config.GetForceLowercaseRouting() {
// only in that state, keep everything else as end-developer registered.
r.Path = strings.ToLower(r.Path)
@@ -225,8 +231,12 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
// the route logs are colorful.
// Note: don't use map, we need to keep registered order, use
// different slices for each method.
collect := func(method string) (methodRoutes []*Route) {
for _, r := range registeredRoutes {
if r.NoLog {
continue
}
if r.Method == method {
methodRoutes = append(methodRoutes, r)
}
@@ -263,7 +273,7 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
// logger.Debugf("API: %d registered %s (", len(registeredRoutes), tr)
// with:
pio.WriteRich(logger.Printer, debugLevel.Title, debugLevel.ColorCode, debugLevel.Style...)
fmt.Fprintf(logger.Printer, " %s %sAPI: %d registered %s (", time.Now().Format(logger.TimeFormat), logger.Prefix, len(registeredRoutes), tr)
fmt.Fprintf(logger.Printer, " %s %sAPI: %d registered %s (", time.Now().Format(logger.TimeFormat), logger.Prefix, len(registeredRoutes)-noLogCount, tr)
//
logger.NewLine = bckpNewLine

View File

@@ -19,6 +19,7 @@ import (
// If any of the following fields are changed then the
// caller should Refresh the router.
type Route struct {
Title string `json"title"` // custom name to replace the method on debug logging.
Name string `json:"name"` // "userRoute"
Description string `json:"description"` // "lists a user"
Method string `json:"method"` // "GET"
@@ -63,6 +64,7 @@ type Route struct {
// OnBuild runs right before BuildHandlers.
OnBuild func(r *Route)
NoLog bool // disables debug logging.
}
// NewRoute returns a new route based on its method,
@@ -349,14 +351,14 @@ func (r *Route) ResolvePath(args ...string) string {
return formattedPath
}
func traceHandlerFile(method, name, line string, number int) string {
func traceHandlerFile(title, name, line string, number int) string {
file := fmt.Sprintf("(%s:%d)", filepath.ToSlash(line), number)
if context.IgnoreHandlerName(name) {
return ""
}
space := strings.Repeat(" ", len(method)+1)
space := strings.Repeat(" ", len(title)+1)
return fmt.Sprintf("\n%s • %s %s", space, name, file)
}
@@ -389,18 +391,22 @@ func traceMethodColor(method string) int {
// * @second_handler ...
// If route and handler line:number locations are equal then the second is ignored.
func (r *Route) Trace(w io.Writer, stoppedIndex int) {
method := r.Method
if method == "" {
method = fmt.Sprintf("%d", r.StatusCode)
title := r.Title
if title == "" {
if r.StatusCode > 0 {
title = fmt.Sprintf("%d", r.StatusCode) // if error code then title is the status code, e.g. 400.
} else {
title = r.Method // else is its method, e.g. GET
}
}
// Color the method.
color := traceMethodColor(method)
color := traceMethodColor(title)
// @method: @path
// space := strings.Repeat(" ", len(http.MethodConnect)-len(method))
// s := fmt.Sprintf("%s: %s", pio.Rich(method, color), path)
pio.WriteRich(w, method, color)
// s := fmt.Sprintf("%s: %s", pio.Rich(title, color), path)
pio.WriteRich(w, title, color)
path := r.Tmpl().Src
if path == "" {
@@ -412,7 +418,7 @@ func (r *Route) Trace(w io.Writer, stoppedIndex int) {
// (@description)
description := r.Description
if description == "" {
if method == MethodNone {
if title == MethodNone {
description = "offline"
}
@@ -469,7 +475,7 @@ func (r *Route) Trace(w io.Writer, stoppedIndex int) {
}
// * @handler_name (@handler_rel_location)
fmt.Fprint(w, traceHandlerFile(r.Method, name, file, line))
fmt.Fprint(w, traceHandlerFile(title, name, file, line))
if stoppedIndex != -1 && stoppedIndex <= len(r.Handlers) {
if i <= stoppedIndex {
pio.WriteRich(w, " ✓", pio.Green)