Skip to content

Commit 738a84f

Browse files
committed
add HTTPCode() for Controller.RenderError()
1 parent 5c52331 commit 738a84f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

controller.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package revel
77
import (
88
"errors"
99
"fmt"
10-
"github.com/revel/revel/logger"
1110
"io"
1211
"net/http"
1312
"os"
@@ -16,6 +15,8 @@ import (
1615
"runtime"
1716
"strings"
1817
"time"
18+
19+
"github.com/revel/revel/logger"
1920
)
2021

2122
// Controller Revel's controller structure that gets embedded in user defined
@@ -41,6 +42,7 @@ type Controller struct {
4142
Validation *Validation // Data validation helpers
4243
Log logger.MultiLogger // Context Logger
4344
}
45+
4446
// The map of controllers, controllers are mapped by using the namespace|controller_name as the key
4547
var controllers = make(map[string]*ControllerType)
4648
var controllerLog = RevelLog.New("section", "controller")
@@ -119,8 +121,16 @@ func (c *Controller) SetCookie(cookie *http.Cookie) {
119121
c.Response.Out.internalHeader.SetCookie(cookie.String())
120122
}
121123

124+
type ErrorCoder interface {
125+
HTTPCode() int
126+
}
127+
122128
func (c *Controller) RenderError(err error) Result {
123-
c.setStatusIfNil(http.StatusInternalServerError)
129+
if coder, ok := err.(ErrorCoder); ok {
130+
c.setStatusIfNil(coder.HTTPCode())
131+
} else {
132+
c.setStatusIfNil(http.StatusInternalServerError)
133+
}
124134

125135
return ErrorResult{c.ViewArgs, err}
126136
}
@@ -486,9 +496,6 @@ func findControllers(appControllerType reflect.Type) (indexes [][]int) {
486496
return
487497
}
488498

489-
490-
491-
492499
// RegisterController registers a Controller and its Methods with Revel.
493500
func RegisterController(c interface{}, methods []*MethodType) {
494501
// De-star the controller type

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy