Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit f6b3dcd

Browse files
Added mime to http, updated error (#7)
2 parents 9bf64e1 + 84dbd25 commit f6b3dcd

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

schema/http/error.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import (
66

77
// NewError creates a new Error instance.
88
func NewError(status Status, message ...string) IError {
9-
if len(message) > 0 {
9+
if len(message) > 1 {
1010
return &Error{status, strings.Join(message, " ")}
1111
}
12-
return &Error{status, status.String()}
13-
}
1412

15-
// Error returns the error message.
16-
func (e *Error) Error() string {
17-
return e.Message
13+
return &Error{Status: status, Message: message[0]}
1814
}
1915

2016
// StatusCode returns the HTTP status code.
2117
func (e *Error) StatusCode() Status {
2218
return e.Status
2319
}
20+
21+
// Error returns the error message.
22+
func (e *Error) Error() string {
23+
return e.Message
24+
}

schema/http/mime.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package http
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"strings"
7+
)
8+
9+
// Common HTTP mimes
10+
const (
11+
MimeInvalid Mime = iota //
12+
MimeTextXML
13+
MimeTextHTML
14+
MimeTextPlain
15+
MimeTextJavaScript
16+
MimeApplicationXML
17+
MimeApplicationJSON
18+
19+
MimeApplicationForm
20+
MimeOctetStream
21+
MimeMultipartForm
22+
23+
MimeTextXMLCharsetUTF8
24+
MimeTextHTMLCharsetUTF8
25+
MimeTextPlainCharsetUTF8
26+
MimeTextJavaScriptCharsetUTF8
27+
MimeApplicationXMLCharsetUTF8
28+
MimeApplicationJSONCharsetUTF8
29+
)
30+
31+
var (
32+
// MimeNames is a map of Mime to string.
33+
MimeNames = map[Mime]string{
34+
MimeTextXML: "text/xml",
35+
MimeTextHTML: "text/html",
36+
MimeTextPlain: "text/plain",
37+
MimeTextJavaScript: "text/javascript",
38+
MimeApplicationXML: "application/xml",
39+
MimeApplicationJSON: "application/json",
40+
41+
MimeApplicationForm: "application/x-www-form-urlencoded",
42+
MimeOctetStream: "application/octet-stream",
43+
MimeMultipartForm: "multipart/form-data",
44+
45+
MimeTextXMLCharsetUTF8: "text/xml; charset=utf-8",
46+
MimeTextHTMLCharsetUTF8: "text/html; charset=utf-8",
47+
MimeTextPlainCharsetUTF8: "text/plain; charset=utf-8",
48+
MimeTextJavaScriptCharsetUTF8: "text/javascript; charset=utf-8",
49+
MimeApplicationXMLCharsetUTF8: "application/xml; charset=utf-8",
50+
MimeApplicationJSONCharsetUTF8: "application/json; charset=utf-8",
51+
}
52+
53+
// ErrMimeInvalid is returned if the HTTP mime is invalid.
54+
ErrMimeInvalid = errors.New("invalid http mime")
55+
)
56+
57+
// String mime to string
58+
func (m Mime) String() string {
59+
return MimeNames[m]
60+
}
61+
62+
// MarshalJSON mime to json
63+
func (m Mime) MarshalJSON() ([]byte, error) {
64+
return []byte(`"` + m.String() + `"`), nil
65+
}
66+
67+
// UnmarshalJSON mime from json
68+
func (m *Mime) UnmarshalJSON(b []byte) error {
69+
*m = ParseMime(string(bytes.Trim(b, `"`)))
70+
71+
return nil
72+
}
73+
74+
// ParseMime parses mime string.
75+
func ParseMime(name string) Mime {
76+
name = strings.ToUpper(name)
77+
for k, v := range MimeNames {
78+
if v == name {
79+
return k
80+
}
81+
}
82+
83+
return MimeInvalid
84+
}
85+
86+
// MustParseMime parses mime string or panics.
87+
func MustParseMime(name string) Mime {
88+
v := ParseMime(name)
89+
if v == MimeInvalid {
90+
panic(ErrMimeInvalid)
91+
}
92+
93+
return v
94+
}

schema/http/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ type (
77
// Method defines an HTTP method.
88
Method uint8
99

10+
// Mime defines an HTTP MIME type.
11+
Mime uint8
12+
1013
// Status defines an HTTP status.
1114
Status uint16
1215

0 commit comments

Comments
 (0)