From 8981b3b9aaf90b58f9efded04b3ba50aebf3326a Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Thu, 22 Mar 2018 08:55:50 -0500 Subject: [PATCH] reduces duplicity on app.go and splits errors.go into errors and error_templates.go --- app.go | 22 ++--- error_templates.go | 125 +++++++++++++++++++++++++ errors.go | 228 --------------------------------------------- 3 files changed, 136 insertions(+), 239 deletions(-) create mode 100644 error_templates.go diff --git a/app.go b/app.go index e147a0bc1..0ef2ac047 100644 --- a/app.go +++ b/app.go @@ -151,17 +151,17 @@ func New(opts Options) *App { routes: RouteList{}, children: []*App{}, } - a.router.NotFoundHandler = http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - c := a.newContext(RouteInfo{}, res, req) - err := errors.Errorf("path not found: %s %s", req.Method, req.URL.Path) - a.ErrorHandlers.Get(404)(404, err, c) - }) - - a.router.MethodNotAllowedHandler = http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - c := a.newContext(RouteInfo{}, res, req) - err := errors.Errorf("method not found: %s %s", req.Method, req.URL.Path) - a.ErrorHandlers.Get(405)(405, err, c) - }) + + notFoundHandler := func(errorf string, code int) http.HandlerFunc { + return func(res http.ResponseWriter, req *http.Request) { + c := a.newContext(RouteInfo{}, res, req) + err := errors.Errorf(errorf, req.Method, req.URL.Path) + a.ErrorHandlers.Get(code)(code, err, c) + } + } + + a.router.NotFoundHandler = http.HandlerFunc(notFoundHandler("path not found: %s %s", 404)) + a.router.MethodNotAllowedHandler = http.HandlerFunc(notFoundHandler("method not found: %s %s", 405)) if a.MethodOverride == nil { a.MethodOverride = MethodOverride diff --git a/error_templates.go b/error_templates.go new file mode 100644 index 000000000..6dac66607 --- /dev/null +++ b/error_templates.go @@ -0,0 +1,125 @@ +package buffalo + +var devErrorTmpl = ` + + + <%= status %> - ERROR! + + + + +
+
+
+ +
+

+ <%= status %> - ERROR! +

+
+
+
+
+ +
+
+
+

Error Trace

+
<%= error %>
+ +

Context

+
<%= inspect(context) %>
+ +

Parameters

+
<%= inspect(params) %>
+ +

Headers

+
<%= inspect(headers) %>
+ +

Form

+
<%= inspect(posted_form) %>
+ +

Routes

+ + + + + + + + + + + + <%= for (r) in routes { %> + + + + + + + <% } %> + + +
METHODPATHNAMEHANDLER
+ <%= r.Method %> + + <%= if (r.Method != "GET" || r.Path ~= "{") { %> + <%= r.Path %> + <% } else { %> + <%= r.Path %> + <% } %> + + <%= r.PathName %> + <%= r.HandlerName %>
+
+
+
Powered by gobuffalo.io
+
+ + +` +var prodErrorTmpl = ` + + + + + + +
+
+

We're Sorry!

+
+

It looks like something went wrong! Don't worry, we are aware of the problem and are looking into it.

+

Sorry if this has caused you any problems. Please check back again later.

+
+ +

powered by gobuffalo.io

+
+ + +` + +var prodNotFoundTmpl = ` + + + + + + +
+
+

Not Found

+
+

The page you’re looking for does not exist, you may have mistyped the address or the page may have been moved.

+
+ +

powered by gobuffalo.io

+
+ + +` diff --git a/errors.go b/errors.go index f56b1ad7b..a3d52f884 100644 --- a/errors.go +++ b/errors.go @@ -145,231 +145,3 @@ func (i inspectHeaders) String() string { sort.Strings(bb) return strings.Join(bb, "\n\n") } - -var devErrorTmpl = ` - - - <%= status %> - ERROR! - - - - -
-
-
- -
-

- <%= status %> - ERROR! -

-
-
-
-
- -
-
-
-

Error Trace

-
<%= error %>
- -

Context

-
<%= inspect(context) %>
- -

Parameters

-
<%= inspect(params) %>
- -

Headers

-
<%= inspect(headers) %>
- -

Form

-
<%= inspect(posted_form) %>
- -

Routes

- - - - - - - - - - - - <%= for (r) in routes { %> - - - - - - - <% } %> - - -
METHODPATHNAMEHANDLER
- <%= r.Method %> - - <%= if (r.Method != "GET" || r.Path ~= "{") { %> - <%= r.Path %> - <% } else { %> - <%= r.Path %> - <% } %> - - <%= r.PathName %> - <%= r.HandlerName %>
-
-
-
Powered by gobuffalo.io
-
- - -` -var prodErrorTmpl = ` - - - - - - -
-
-

We're Sorry!

-
-

It looks like something went wrong! Don't worry, we are aware of the problem and are looking into it.

-

Sorry if this has caused you any problems. Please check back again later.

-
- -

powered by gobuffalo.io

-
- - -` - -var prodNotFoundTmpl = ` - - - - - - -
-
-

Not Found

-
-

The page you’re looking for does not exist, you may have mistyped the address or the page may have been moved.

-
- -

powered by gobuffalo.io

-
- - -`