From b6f32b12516dc9623a721bd527e25ad7c7420f98 Mon Sep 17 00:00:00 2001
From: RandomChars <random@chars.jp>
Date: Mon, 4 Oct 2021 22:34:45 +0900
Subject: [PATCH] update readme, unmarshal errors properly

---
 README.md         |  2 +-
 client/request.go | 16 +++++++++++++++-
 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
-- 
GitLab