Skip to content

Commit e5e767f

Browse files
authored
refactor: remove errors package (#43)
1 parent 74158e5 commit e5e767f

15 files changed

+120
-344
lines changed

context.go

+23-29
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,34 @@ func (c *Context) QueryParams() url.Values {
7878
return c.request.URL.Query()
7979
}
8080

81+
// mustWrite writes raw bytes to the response.
82+
func (c *Context) mustWrite(blob []byte) {
83+
if _, err := c.Response().Write(blob); err != nil {
84+
panic(err)
85+
}
86+
}
87+
8188
// JSON sends JSON response with the given status code.
82-
//
83-
// Returns an error if an error happened during sending response otherwise returns nil.
84-
func (c *Context) JSON(code int, obj any) error {
89+
func (c *Context) JSON(code int, obj any) {
8590
c.writeContentType("application/json")
8691
c.response.WriteHeader(code)
87-
return c.kid.jsonSerializer.Write(c.Response(), obj, "")
92+
c.kid.jsonSerializer.Write(c.Response(), obj, "")
8893
}
8994

9095
// JSONIndent sends JSON response with the given status code.
9196
// Sends response with the given indent.
92-
//
93-
// Returns an error if an error happened during sending response otherwise returns nil.
94-
func (c *Context) JSONIndent(code int, obj any, indent string) error {
97+
func (c *Context) JSONIndent(code int, obj any, indent string) {
9598
c.writeContentType("application/json")
9699
c.response.WriteHeader(code)
97-
return c.kid.jsonSerializer.Write(c.Response(), obj, indent)
100+
c.kid.jsonSerializer.Write(c.Response(), obj, indent)
98101
}
99102

100103
// JSONByte sends JSON response with the given status code.
101104
// Writes JSON blob untouched to response.
102-
func (c *Context) JSONByte(code int, blob []byte) error {
105+
func (c *Context) JSONByte(code int, blob []byte) {
103106
c.writeContentType("application/json")
104107
c.response.WriteHeader(code)
105-
_, err := c.Response().Write(blob)
106-
return err
108+
c.mustWrite(blob)
107109
}
108110

109111
// ReadJSON reads request's body as JSON and stores it in the given object.
@@ -115,29 +117,26 @@ func (c *Context) ReadJSON(out any) error {
115117
// XML sends XML response with the given status code.
116118
//
117119
// Returns an error if an error happened during sending response otherwise returns nil.
118-
func (c *Context) XML(code int, obj any) error {
120+
func (c *Context) XML(code int, obj any) {
119121
c.writeContentType("application/xml")
120122
c.response.WriteHeader(code)
121-
return c.kid.xmlSerializer.Write(c.Response(), obj, "")
123+
c.kid.xmlSerializer.Write(c.Response(), obj, "")
122124
}
123125

124126
// XMLIndent sends XML response with the given status code.
125127
// Sends response with the given indent.
126-
//
127-
// Returns an error if an error happened during sending response otherwise returns nil.
128-
func (c *Context) XMLIndent(code int, obj any, indent string) error {
128+
func (c *Context) XMLIndent(code int, obj any, indent string) {
129129
c.writeContentType("application/xml")
130130
c.response.WriteHeader(code)
131-
return c.kid.xmlSerializer.Write(c.Response(), obj, indent)
131+
c.kid.xmlSerializer.Write(c.Response(), obj, indent)
132132
}
133133

134134
// XMLByte sends XML response with the given status code.
135135
// Writes JSON blob untouched to response.
136-
func (c *Context) XMLByte(code int, blob []byte) error {
136+
func (c *Context) XMLByte(code int, blob []byte) {
137137
c.writeContentType("application/xml")
138138
c.response.WriteHeader(code)
139-
_, err := c.Response().Write(blob)
140-
return err
139+
c.mustWrite(blob)
141140
}
142141

143142
// ReadXML reads request's body as XML and stores it in the given object.
@@ -150,22 +149,17 @@ func (c *Context) ReadXML(out any) error {
150149
//
151150
// tpl must be a relative path to templates root directory.
152151
// Defaults to "templates/".
153-
//
154-
// Returns an error if an error happened during sending response otherwise returns nil.
155-
func (c *Context) HTML(code int, tpl string, data any) error {
152+
func (c *Context) HTML(code int, tpl string, data any) {
156153
c.writeContentType("text/html")
157154
c.response.WriteHeader(code)
158-
return c.kid.htmlRenderer.RenderHTML(c.Response(), tpl, data)
155+
c.kid.htmlRenderer.RenderHTML(c.Response(), tpl, data)
159156
}
160157

161158
// HTMLString sends bare string as HTML response with the given status code.
162-
//
163-
// Returns an error if an error happened during sending response otherwise returns nil.
164-
func (c *Context) HTMLString(code int, tpl string) error {
159+
func (c *Context) HTMLString(code int, tpl string) {
165160
c.writeContentType("text/html")
166161
c.response.WriteHeader(code)
167-
_, err := c.Response().Write([]byte(tpl))
168-
return err
162+
c.mustWrite([]byte(tpl))
169163
}
170164

171165
// NoContent returns an empty response with the given status code.

context_test.go

+50-46
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kid
33
import (
44
"encoding/json"
55
"encoding/xml"
6+
"errors"
67
"fmt"
78
"net/http"
89
"net/http/httptest"
@@ -11,11 +12,18 @@ import (
1112
"strings"
1213
"testing"
1314

14-
"github.com/mojixcoder/kid/errors"
1515
htmlrenderer "github.com/mojixcoder/kid/html_renderer"
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19+
type errWriter struct {
20+
*httptest.ResponseRecorder
21+
}
22+
23+
func (errWriter) Write(blob []byte) (int, error) {
24+
return 0, errors.New("new err")
25+
}
26+
1927
type person struct {
2028
Name string `json:"name" xml:"name"`
2129
Age int `json:"age" xml:"age"`
@@ -238,11 +246,26 @@ func TestContext_ReadJSON(t *testing.T) {
238246
ctx.reset(req, res)
239247

240248
var p2 person
241-
httpErr := ctx.ReadJSON(&p2).(*errors.HTTPError)
249+
err = ctx.ReadJSON(&p2)
250+
251+
assert.Error(t, err)
252+
}
253+
254+
func TestContext_mustWrite(t *testing.T) {
255+
ctx := newContext(New())
256+
257+
res := httptest.NewRecorder()
258+
ctx.reset(nil, res)
259+
260+
ctx.mustWrite([]byte("byte"))
261+
262+
assert.Equal(t, "byte", res.Body.String())
263+
264+
ctx.reset(nil, errWriter{res})
242265

243-
assert.Error(t, httpErr)
244-
assert.Error(t, httpErr.Err)
245-
assert.Equal(t, http.StatusBadRequest, httpErr.Code)
266+
assert.Panics(t, func() {
267+
ctx.mustWrite([]byte("byte"))
268+
})
246269
}
247270

248271
func TestContext_JSON(t *testing.T) {
@@ -253,9 +276,8 @@ func TestContext_JSON(t *testing.T) {
253276
ctx.reset(nil, res)
254277

255278
p := person{Name: "foo", Age: 1999}
256-
err := ctx.JSON(http.StatusCreated, &p)
279+
ctx.JSON(http.StatusCreated, &p)
257280

258-
assert.NoError(t, err)
259281
assert.Equal(t, http.StatusCreated, res.Code)
260282
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
261283
assert.Equal(t, "{\"name\":\"foo\",\"age\":1999}\n", res.Body.String())
@@ -264,11 +286,9 @@ func TestContext_JSON(t *testing.T) {
264286

265287
ctx.reset(nil, res)
266288

267-
httpErr := ctx.JSON(http.StatusCreated, make(chan bool)).(*errors.HTTPError)
268-
269-
assert.Error(t, httpErr)
270-
assert.Error(t, httpErr.Err)
271-
assert.Equal(t, http.StatusInternalServerError, httpErr.Code)
289+
assert.Panics(t, func() {
290+
ctx.JSON(http.StatusCreated, make(chan bool))
291+
})
272292
}
273293

274294
func TestContext_JSONIndent(t *testing.T) {
@@ -279,9 +299,8 @@ func TestContext_JSONIndent(t *testing.T) {
279299
ctx.reset(nil, res)
280300

281301
p := person{Name: "foo", Age: 1999}
282-
err := ctx.JSONIndent(http.StatusCreated, &p, " ")
302+
ctx.JSONIndent(http.StatusCreated, &p, " ")
283303

284-
assert.NoError(t, err)
285304
assert.Equal(t, http.StatusCreated, res.Code)
286305
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
287306
assert.Equal(t, "{\n \"name\": \"foo\",\n \"age\": 1999\n}\n", res.Body.String())
@@ -290,11 +309,9 @@ func TestContext_JSONIndent(t *testing.T) {
290309

291310
ctx.reset(nil, res)
292311

293-
httpErr := ctx.JSONIndent(http.StatusCreated, make(chan bool), " ").(*errors.HTTPError)
294-
295-
assert.Error(t, httpErr)
296-
assert.Error(t, httpErr.Err)
297-
assert.Equal(t, http.StatusInternalServerError, httpErr.Code)
312+
assert.Panics(t, func() {
313+
ctx.JSONIndent(http.StatusCreated, make(chan bool), " ")
314+
})
298315
}
299316

300317
func TestContext_JSONByte(t *testing.T) {
@@ -309,9 +326,8 @@ func TestContext_JSONByte(t *testing.T) {
309326
blob, err := json.Marshal(p)
310327
assert.NoError(t, err)
311328

312-
err = ctx.JSONByte(http.StatusOK, blob)
329+
ctx.JSONByte(http.StatusOK, blob)
313330

314-
assert.NoError(t, err)
315331
assert.Equal(t, http.StatusOK, res.Code)
316332
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
317333
assert.Equal(t, "{\"name\":\"foo\",\"age\":1999}", res.Body.String())
@@ -335,11 +351,9 @@ func TestContext_ReadXML(t *testing.T) {
335351
ctx.reset(req, nil)
336352

337353
var p2 person
338-
httpErr := ctx.ReadXML(&p2).(*errors.HTTPError)
354+
err = ctx.ReadXML(&p2)
339355

340-
assert.Error(t, httpErr)
341-
assert.Error(t, httpErr.Err)
342-
assert.Equal(t, http.StatusBadRequest, httpErr.Code)
356+
assert.Error(t, err)
343357
}
344358

345359
func TestContext_XML(t *testing.T) {
@@ -350,9 +364,8 @@ func TestContext_XML(t *testing.T) {
350364
ctx.reset(nil, res)
351365

352366
p := person{Name: "foo", Age: 1999}
353-
err := ctx.XML(http.StatusCreated, &p)
367+
ctx.XML(http.StatusCreated, &p)
354368

355-
assert.NoError(t, err)
356369
assert.Equal(t, http.StatusCreated, res.Code)
357370
assert.Equal(t, "application/xml", res.Header().Get("Content-Type"))
358371
assert.Equal(t, "<person><name>foo</name><age>1999</age></person>", res.Body.String())
@@ -361,11 +374,9 @@ func TestContext_XML(t *testing.T) {
361374

362375
ctx.reset(nil, res)
363376

364-
httpErr := ctx.XML(http.StatusCreated, make(chan bool)).(*errors.HTTPError)
365-
366-
assert.Error(t, httpErr)
367-
assert.Error(t, httpErr.Err)
368-
assert.Equal(t, http.StatusInternalServerError, httpErr.Code)
377+
assert.Panics(t, func() {
378+
ctx.XML(http.StatusCreated, make(chan bool))
379+
})
369380
}
370381

371382
func TestContext_XMLIndent(t *testing.T) {
@@ -376,9 +387,8 @@ func TestContext_XMLIndent(t *testing.T) {
376387
ctx.reset(nil, res)
377388

378389
p := person{Name: "foo", Age: 1999}
379-
err := ctx.XMLIndent(http.StatusCreated, &p, " ")
390+
ctx.XMLIndent(http.StatusCreated, &p, " ")
380391

381-
assert.NoError(t, err)
382392
assert.Equal(t, http.StatusCreated, res.Code)
383393
assert.Equal(t, "application/xml", res.Header().Get("Content-Type"))
384394
assert.Equal(t, "<person>\n <name>foo</name>\n <age>1999</age>\n</person>", res.Body.String())
@@ -387,11 +397,9 @@ func TestContext_XMLIndent(t *testing.T) {
387397

388398
ctx.reset(nil, res)
389399

390-
httpErr := ctx.XMLIndent(http.StatusCreated, make(chan bool), " ").(*errors.HTTPError)
391-
392-
assert.Error(t, httpErr)
393-
assert.Error(t, httpErr.Err)
394-
assert.Equal(t, http.StatusInternalServerError, httpErr.Code)
400+
assert.Panics(t, func() {
401+
ctx.XMLIndent(http.StatusCreated, make(chan bool), " ")
402+
})
395403
}
396404

397405
func TestContext_XMLByte(t *testing.T) {
@@ -406,9 +414,8 @@ func TestContext_XMLByte(t *testing.T) {
406414
blob, err := xml.Marshal(p)
407415
assert.NoError(t, err)
408416

409-
err = ctx.XMLByte(http.StatusOK, blob)
417+
ctx.XMLByte(http.StatusOK, blob)
410418

411-
assert.NoError(t, err)
412419
assert.Equal(t, http.StatusOK, res.Code)
413420
assert.Equal(t, "application/xml", res.Header().Get("Content-Type"))
414421
assert.Equal(t, "<person><name>foo</name><age>1999</age></person>", res.Body.String())
@@ -425,15 +432,14 @@ func TestContext_HTML(t *testing.T) {
425432
res := httptest.NewRecorder()
426433
ctx.reset(nil, res)
427434

428-
err := ctx.HTML(http.StatusAccepted, "index.html", nil)
435+
ctx.HTML(http.StatusAccepted, "index.html", nil)
429436

430437
newLine := getNewLineStr()
431438
expectedRes := fmt.Sprintf(
432439
"%s<html><body>%s<p>content</p>%s</body></html>%s",
433440
newLine, newLine, newLine, newLine,
434441
)
435442

436-
assert.NoError(t, err)
437443
assert.Equal(t, http.StatusAccepted, res.Code)
438444
assert.Equal(t, expectedRes, res.Body.String())
439445
assert.Equal(t, "text/html", res.Header().Get("Content-Type"))
@@ -445,11 +451,9 @@ func TestContext_HTMLString(t *testing.T) {
445451
res := httptest.NewRecorder()
446452
ctx.reset(nil, res)
447453

448-
err := ctx.HTMLString(http.StatusAccepted, "<p>Hello</p>")
454+
ctx.HTMLString(http.StatusAccepted, "<p>Hello</p>")
449455

450-
assert.NoError(t, err)
451456
assert.Equal(t, http.StatusAccepted, res.Code)
452457
assert.Equal(t, "<p>Hello</p>", res.Body.String())
453458
assert.Equal(t, "text/html", res.Header().Get("Content-Type"))
454-
455459
}

errors/errors.go

-54
This file was deleted.

0 commit comments

Comments
 (0)