Skip to content

Commit b743050

Browse files
committed
- Updated unit test cases
- Changed unhandled error to runtime error (go like keyword) - Not found route will execute all preSend and postSend tasks
1 parent f9a3e27 commit b743050

File tree

7 files changed

+19
-18
lines changed

7 files changed

+19
-18
lines changed

api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ func (api *API) UnhandledException(handle Handler) {
153153

154154
// error variables to handle expected errors
155155
var (
156-
ErrCodeNotFound = "URL_NOT_FOUND"
157-
ErrCodeUncaughtException = "UNCAUGHT_EXCEPTION"
156+
ErrCodeNotFound = "URL_NOT_FOUND"
157+
ErrCodeRuntimeError = "RUNTIME_ERROR"
158158
)
159159

160160
// It's required handle for http module.
@@ -175,7 +175,7 @@ func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
175175
err := recover()
176176
if err != nil {
177177
if !ctx.end {
178-
ctx.code = ErrCodeUncaughtException
178+
ctx.code = ErrCodeRuntimeError
179179
ctx.err = fmt.Errorf("%v", err)
180180
ctx.unhandledException()
181181
}

api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestAPI_Patch(t *testing.T) {
9090
validateRoute("Patch", "PATCH", "/:uid", t)
9191
}
9292

93-
func TestAPI_Exception(t *testing.T) {
93+
func TestAPI_OnError(t *testing.T) {
9494
a.OnError("UID_NOT_FOUND", handle)
9595

9696
flag := true

context.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,16 @@ func (ctx *Context) unhandledException() {
216216

217217
// NOT FOUND handler
218218
if ctx.code == ErrCodeNotFound {
219-
http.NotFound(ctx.Response, ctx.Request)
220-
return
219+
ctx.Status(http.StatusNotFound)
221220
}
222221

223222
if ctx.code != "" || ctx.err != nil {
224-
msg := ctx.code
223+
msg := "Error Code: " + ctx.code
225224
if ctx.err != nil {
226-
msg += "\nError: " + ctx.err.Error()
225+
msg += "\nError Message: " + ctx.err.Error()
227226
}
228227
ctx.SetHeader("Content-Type", "text/plain;charset=UTF-8")
229-
if ctx.status > 400 {
228+
if ctx.status < 400 {
230229
ctx.Status(http.StatusInternalServerError)
231230
}
232231
ctx.Write([]byte(msg))
@@ -238,11 +237,9 @@ func (ctx *Context) recover() {
238237
err := recover()
239238
if err != nil {
240239
//TODO: debugger mode
241-
log.Println("Unhandled Error: ", err)
240+
log.Println("Runtime Error: ", err)
242241
if !ctx.requestSent {
243-
ctx.Response.WriteHeader(http.StatusInternalServerError)
244-
ctx.Response.Header().Set("Content-Type", "text/plain;charset=UTF-8")
245-
_, _ = ctx.Response.Write([]byte("Internal Server Error"))
242+
http.Error(ctx.Response, "Internal Server Error", http.StatusInternalServerError)
246243
}
247244
}
248245
}

examples/server.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func main() {
3232
s := time.Now().UnixNano()
3333
ctx.PreSend(func() {
3434
x := time.Now().UnixNano() - s
35-
ctx.SetHeader("X-Runtime", strconv.FormatInt(x/int64(time.Millisecond), 10))
35+
ctx.SetHeader("X-Runtime", strconv.FormatInt(x/int64(time.Microsecond), 10))
3636
})
3737
})
3838

@@ -52,7 +52,11 @@ func main() {
5252

5353
fmt.Println("Starting server.")
5454

55-
err := http.ListenAndServe(":8080", api)
55+
server := http.Server{
56+
Addr: ":8080",
57+
Handler: api,
58+
}
5659

60+
err := server.ListenAndServe()
5761
fmt.Println(err)
5862
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/go-rs/rest-api-framework
22

3-
go 1.12
3+
go 1.13

namespace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestNamespace_Patch(t *testing.T) {
8888
validateNsRoute("Patch", "PATCH", "/test/:uid", t)
8989
}
9090

91-
func TestNamespace_Exception(t *testing.T) {
91+
func TestNamespace_OnError(t *testing.T) {
9292
ns.OnError("UID_NOT_FOUND", handle)
9393

9494
flag := true

render/text_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestText_Write(t *testing.T) {
2020
t.Error("Render text is not valid")
2121
}
2222

23-
if w.Header().Get("Content-Type") != "text/plain" {
23+
if w.Header().Get("Content-Type") != "text/plain;charset=UTF-8" {
2424
t.Error("Content-Type Header is not set.")
2525
}
2626
}

0 commit comments

Comments
 (0)