summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandomChars <random@chars.jp>2021-10-04 22:34:45 +0900
committerRandomChars <random@chars.jp>2021-10-04 22:34:45 +0900
commitb6f32b12516dc9623a721bd527e25ad7c7420f98 (patch)
tree80fe58c7d61d1ee5e286bcf75b5c5b04cb6d2222
parentb9bf31bf6180a79f1f007cc2a23df3992f2265a7 (diff)
update readme, unmarshal errors properlyv1.3.8
-rw-r--r--README.md2
-rw-r--r--client/request.go16
2 files changed, 16 insertions, 2 deletions
diff --git a/README.md b/README.md
index 811db81..9aba6a5 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
Image Board
-----------
-Taggable image board in 1209 lines of code.
+Taggable image board in 1272 lines of code.
Get it:
```shell
diff --git a/client/request.go b/client/request.go
index ecb3569..93ada62 100644
--- a/client/request.go
+++ b/client/request.go
@@ -3,9 +3,11 @@ package client
import (
"bytes"
"encoding/json"
+ "errors"
"fmt"
"io"
"net/http"
+ "random.chars.jp/git/image-board/api"
)
func (r *Remote) send(req *http.Request) (*http.Response, error) {
@@ -94,7 +96,19 @@ func unmarshal(reader io.ReadCloser, v interface{}) error {
fmt.Printf("Error closing reader after unmarshalling, %s\n", err)
}
}()
- if err := json.NewDecoder(reader).Decode(v); err != nil {
+
+ var data []byte
+ if d, err := io.ReadAll(reader); err != nil {
+ return err
+ } else {
+ data = d
+ }
+
+ if err := json.Unmarshal(data, v); err != nil {
+ var errPayload api.Error
+ if tryErr := json.Unmarshal(data, &errPayload); tryErr == nil {
+ return errors.New(errPayload.Error)
+ }
return err
}
return nil