Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10668492
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
View Options
diff --git a/response.go b/response.go
index fe097bb..d92f9b4 100644
--- a/response.go
+++ b/response.go
@@ -1,76 +1,80 @@
package impart
import (
"encoding/json"
"net/http"
"strconv"
)
type (
// Envelope contains metadata and optional data for a response object.
// Responses will always contain a status code and either:
// - response Data on a 2xx response, or
// - an ErrorMessage on non-2xx responses
//
// ErrorType is not currently used.
Envelope struct {
Code int `json:"code"`
ErrorType string `json:"error_type,omitempty"`
ErrorMessage string `json:"error_msg,omitempty"`
Data interface{} `json:"data,omitempty"`
}
)
func writeBody(w http.ResponseWriter, body []byte, status int, contentType string) error {
w.Header().Set("Content-Type", contentType+"; charset=UTF-8")
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(status)
_, err := w.Write(body)
return err
}
func RenderActivityJSON(w http.ResponseWriter, value interface{}, status int) error {
body, err := json.Marshal(value)
if err != nil {
return err
}
return writeBody(w, body, status, "application/activity+json")
}
func renderJSON(w http.ResponseWriter, value interface{}, status int) error {
body, err := json.Marshal(value)
if err != nil {
return err
}
return writeBody(w, body, status, "application/json")
}
func renderString(w http.ResponseWriter, status int, msg string) error {
return writeBody(w, []byte(msg), status, "text/plain")
}
// WriteSuccess writes the successful data and metadata to the ResponseWriter as
// JSON.
func WriteSuccess(w http.ResponseWriter, data interface{}, status int) error {
env := &Envelope{
Code: status,
Data: data,
}
return renderJSON(w, env, status)
}
// WriteError writes the error to the ResponseWriter as JSON.
func WriteError(w http.ResponseWriter, e HTTPError) error {
+ status := e.Status
+ if status == 0 {
+ status = 500
+ }
env := &Envelope{
- Code: e.Status,
+ Code: status,
ErrorMessage: e.Message,
}
- return renderJSON(w, env, e.Status)
+ return renderJSON(w, env, status)
}
// WriteRedirect sends a redirect
func WriteRedirect(w http.ResponseWriter, e HTTPError) int {
w.Header().Set("Location", e.Message)
w.WriteHeader(e.Status)
return e.Status
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, May 15, 2:58 AM (20 m, 48 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3239396
Attached To
rI impart
Event Timeline
Log In to Comment