diff --git a/README.md b/README.md index 811db81b5db3fb582aba0c2a027b0d51d1f5247a..9aba6a5004a7bf6a15e26d3e0191ca54267dd569 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 ecb3569afdf09d9d03313d2d4782f41e32437558..93ada62bd2297019e9a8c9377375812b7849c6d3 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