Skip to content

Commit 932455b

Browse files
authored
chore: propagate http error in response (#7)
* fix: change variable names * refactor: make HTTP error be propagated if differs from 200 * docs: add LICENSE * refactor: add go.mod
1 parent 3aab19e commit 932455b

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Ufirst
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

client.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"io/ioutil"
89
"net/http"
910
"net/url"
@@ -61,21 +62,21 @@ func (c *Client) Run(method string, params interface{}, result interface{}, opts
6162
return err
6263
}
6364

64-
jsonResponse, err := c.sendJSONRequest(jsonRequest, opts...)
65+
httpResponse, err := c.sendJSONRequest(jsonRequest, opts...)
6566
if err != nil {
6667
return err
6768
}
6869

69-
response := NewResponse()
70-
response.Result = result
70+
jsonRPCResponse := NewResponse()
71+
jsonRPCResponse.Result = result
7172

72-
err = json.Unmarshal(jsonResponse, &response)
73+
err = json.Unmarshal(httpResponse, &jsonRPCResponse)
7374
if err != nil {
7475
return err
7576
}
7677

77-
if response.hasError() {
78-
return errors.New(response.Error.Message)
78+
if jsonRPCResponse.hasError() {
79+
return errors.New(jsonRPCResponse.Error.Message)
7980
}
8081

8182
return nil
@@ -104,8 +105,6 @@ func (c *Client) Notify(method string, params interface{}, opts ...NotifyOptions
104105
}
105106

106107
func (c *Client) sendJSONRequest(jsonRequest []byte, opts ...RunOptions) ([]byte, error) {
107-
var jsonResponse []byte
108-
109108
httpRequest, err := http.NewRequest("POST", c.serverURL, strings.NewReader(string(jsonRequest)))
110109
httpRequest.Header.Set("Content-Type", "application/json")
111110
httpRequest.Header.Set("Content-Length", "")
@@ -121,15 +120,19 @@ func (c *Client) sendJSONRequest(jsonRequest []byte, opts ...RunOptions) ([]byte
121120

122121
httpResponse, err := c.httpClient.Do(httpRequest)
123122
if err != nil {
124-
return jsonResponse, err
123+
return nil, err
125124
}
126125

127126
defer httpResponse.Body.Close()
128127

129-
jsonResponse, err = ioutil.ReadAll(httpResponse.Body)
128+
httpResponseBody, err := ioutil.ReadAll(httpResponse.Body)
130129
if err != nil {
131-
return jsonResponse, err
130+
return httpResponseBody, err
131+
}
132+
133+
if httpResponse.StatusCode >= http.StatusBadRequest {
134+
return nil, fmt.Errorf("received HTTP status %d with body %s", httpResponse.StatusCode, httpResponseBody)
132135
}
133136

134-
return jsonResponse, nil
137+
return httpResponseBody, nil
135138
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/qurami/gojsonrpc
2+
3+
go 1.15

0 commit comments

Comments
 (0)