docd: return error messages in the response

This commit is contained in:
David Howden
2018-02-07 16:45:20 +11:00
parent 142d49c1b0
commit eedabc494f
3 changed files with 7 additions and 14 deletions
+1
View File
@@ -78,6 +78,7 @@ type Response struct {
Body string `json:"body"`
Meta map[string]string `json:"meta"`
MSecs uint32 `json:"msecs"`
Error string `json:"error"`
}
// Convert a file from a local path using the http client
+1
View File
@@ -16,6 +16,7 @@ type Response struct {
Body string `json:"body"`
Meta map[string]string `json:"meta"`
MSecs uint32 `json:"msecs"`
Error string `json:"error"`
}
// MimeTypeByExtension returns a mimetype for the given extension, or
+5 -14
View File
@@ -11,13 +11,6 @@ import (
"code.sajari.com/docconv"
)
// Response payload sent back to the requestor
type Response struct {
Body string `json:"body"`
Meta map[string]string `json:"meta"`
MSecs uint32 `json:"msecs"`
}
var (
inputPath = flag.String("input", "", "The file path to convert and exit; no server")
listenAddr = flag.String("addr", ":8888", "The address to listen on (e.g. 127.0.0.1:8888)")
@@ -135,17 +128,15 @@ func serve() {
data, err := docconv.Convert(file, mimeType, readability)
if err != nil {
log.Printf("error converting data: %v", err)
return
data = &docconv.Response{
Error: err.Error(),
}
}
b, err := json.Marshal(data)
if err != nil {
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("error marshaling JSON data: %v", err)
return
}
if *logLevel >= 2 {
log.Println(string(b))
}
fmt.Fprintf(w, "%s", b)
})
// Start webserver