Skip to content

Commit 2e0d366

Browse files
committed
Add api error handling
1 parent 6e77466 commit 2e0d366

10 files changed

+187
-115
lines changed

block_test.go

+27-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notionapi_test
33
import (
44
"context"
55
"github.com/jomei/notionapi"
6+
"net/http"
67
"reflect"
78
"testing"
89
"time"
@@ -15,24 +16,26 @@ func TestBlockClient(t *testing.T) {
1516
}
1617
t.Run("GetChildren", func(t *testing.T) {
1718
tests := []struct {
18-
name string
19-
filePath string
20-
id notionapi.BlockID
21-
len int
22-
wantErr bool
23-
err error
19+
name string
20+
filePath string
21+
statusCode int
22+
id notionapi.BlockID
23+
len int
24+
wantErr bool
25+
err error
2426
}{
2527
{
26-
name: "returns blocks by id of parent block",
27-
id: "some_id",
28-
filePath: "testdata/block_get_children.json",
29-
len: 2,
28+
name: "returns blocks by id of parent block",
29+
id: "some_id",
30+
statusCode: http.StatusOK,
31+
filePath: "testdata/block_get_children.json",
32+
len: 2,
3033
},
3134
}
3235

3336
for _, tt := range tests {
3437
t.Run(tt.name, func(t *testing.T) {
35-
c := newMockedClient(t, tt.filePath)
38+
c := newMockedClient(t, tt.filePath, tt.statusCode)
3639
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
3740
got, err := client.Block.GetChildren(context.Background(), tt.id, nil)
3841

@@ -50,18 +53,20 @@ func TestBlockClient(t *testing.T) {
5053

5154
t.Run("AppendChildren", func(t *testing.T) {
5255
tests := []struct {
53-
name string
54-
filePath string
55-
id notionapi.BlockID
56-
request *notionapi.AppendBlockChildrenRequest
57-
want *notionapi.ChildPageBlock
58-
wantErr bool
59-
err error
56+
name string
57+
filePath string
58+
statusCode int
59+
id notionapi.BlockID
60+
request *notionapi.AppendBlockChildrenRequest
61+
want *notionapi.ChildPageBlock
62+
wantErr bool
63+
err error
6064
}{
6165
{
62-
name: "returns blocks by id of parent block",
63-
id: "some_id",
64-
filePath: "testdata/block_append_children.json",
66+
name: "returns blocks by id of parent block",
67+
id: "some_id",
68+
filePath: "testdata/block_append_children.json",
69+
statusCode: http.StatusOK,
6570
request: &notionapi.AppendBlockChildrenRequest{
6671
Children: []notionapi.Block{
6772
&notionapi.Heading2Block{
@@ -96,7 +101,7 @@ func TestBlockClient(t *testing.T) {
96101

97102
for _, tt := range tests {
98103
t.Run(tt.name, func(t *testing.T) {
99-
c := newMockedClient(t, tt.filePath)
104+
c := newMockedClient(t, tt.filePath, tt.statusCode)
100105
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
101106
got, err := client.Block.AppendChildren(context.Background(), tt.id, tt.request)
102107

client.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"github.com/pkg/errors"
98
"io"
109
"net/http"
1110
"net/url"
@@ -119,7 +118,13 @@ func (c *Client) request(ctx context.Context, method string, urlStr string, quer
119118
}
120119

121120
if res.StatusCode != http.StatusOK {
122-
return nil, errors.Errorf("http status: %d", res.StatusCode)
121+
var apiErr Error
122+
err = json.NewDecoder(res.Body).Decode(&apiErr)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
return nil, &apiErr
123128
}
124129

125130
return res, nil

client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ func newTestClient(fn RoundTripFunc) *http.Client {
2222
}
2323

2424
// newMockedClient returns *http.Client which responds with content from given file
25-
func newMockedClient(t *testing.T, requestMockFile string) *http.Client {
25+
func newMockedClient(t *testing.T, requestMockFile string, statusCode int) *http.Client {
2626
return newTestClient(func(req *http.Request) *http.Response {
2727
b, err := os.Open(requestMockFile)
2828
if err != nil {
2929
t.Fatal(err)
3030
}
3131

3232
resp := &http.Response{
33-
StatusCode: http.StatusOK,
33+
StatusCode: statusCode,
3434
Body: b,
3535
Header: make(http.Header),
3636
}

const.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const (
77
ObjectTypeList ObjectType = "list"
88
ObjectTypeText ObjectType = "text"
99
ObjectTypeUser ObjectType = "user"
10+
ObjectTypeError ObjectType = "error"
1011
)
1112

1213
const (

database_test.go

+36-30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notionapi_test
33
import (
44
"context"
55
"github.com/jomei/notionapi"
6+
"net/http"
67
"reflect"
78
"testing"
89
"time"
@@ -16,17 +17,19 @@ func TestDatabaseClient(t *testing.T) {
1617

1718
t.Run("Get", func(t *testing.T) {
1819
tests := []struct {
19-
name string
20-
filePath string
21-
id notionapi.DatabaseID
22-
want *notionapi.Database
23-
wantErr bool
24-
err error
20+
name string
21+
filePath string
22+
statusCode int
23+
id notionapi.DatabaseID
24+
want *notionapi.Database
25+
wantErr bool
26+
err error
2527
}{
2628
{
27-
name: "returns database by id",
28-
id: "some_id",
29-
filePath: "testdata/database_get.json",
29+
name: "returns database by id",
30+
id: "some_id",
31+
filePath: "testdata/database_get.json",
32+
statusCode: http.StatusOK,
3033
want: &notionapi.Database{
3134
Object: notionapi.ObjectTypeDatabase,
3235
ID: "some_id",
@@ -69,8 +72,7 @@ func TestDatabaseClient(t *testing.T) {
6972
}
7073
for _, tt := range tests {
7174
t.Run(tt.name, func(t *testing.T) {
72-
73-
c := newMockedClient(t, tt.filePath)
75+
c := newMockedClient(t, tt.filePath, tt.statusCode)
7476
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
7577
got, err := client.Database.Get(context.Background(), tt.id)
7678

@@ -89,15 +91,17 @@ func TestDatabaseClient(t *testing.T) {
8991

9092
t.Run("List", func(t *testing.T) {
9193
tests := []struct {
92-
name string
93-
filePath string
94-
want *notionapi.DatabaseListResponse
95-
wantErr bool
96-
err error
94+
name string
95+
filePath string
96+
statusCode int
97+
want *notionapi.DatabaseListResponse
98+
wantErr bool
99+
err error
97100
}{
98101
{
99-
name: "returns list of databases",
100-
filePath: "testdata/database_list.json",
102+
name: "returns list of databases",
103+
filePath: "testdata/database_list.json",
104+
statusCode: http.StatusOK,
101105
want: &notionapi.DatabaseListResponse{
102106
Object: notionapi.ObjectTypeList,
103107
Results: []notionapi.Database{
@@ -128,7 +132,7 @@ func TestDatabaseClient(t *testing.T) {
128132

129133
for _, tt := range tests {
130134
t.Run(tt.name, func(t *testing.T) {
131-
c := newMockedClient(t, tt.filePath)
135+
c := newMockedClient(t, tt.filePath, tt.statusCode)
132136
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
133137

134138
got, err := client.Database.List(context.Background(), nil)
@@ -147,18 +151,20 @@ func TestDatabaseClient(t *testing.T) {
147151

148152
t.Run("Query", func(t *testing.T) {
149153
tests := []struct {
150-
name string
151-
filePath string
152-
id notionapi.DatabaseID
153-
request *notionapi.DatabaseQueryRequest
154-
want *notionapi.DatabaseQueryResponse
155-
wantErr bool
156-
err error
154+
name string
155+
filePath string
156+
statusCode int
157+
id notionapi.DatabaseID
158+
request *notionapi.DatabaseQueryRequest
159+
want *notionapi.DatabaseQueryResponse
160+
wantErr bool
161+
err error
157162
}{
158163
{
159-
name: "returns query results",
160-
id: "some_id",
161-
filePath: "testdata/database_query.json",
164+
name: "returns query results",
165+
id: "some_id",
166+
filePath: "testdata/database_query.json",
167+
statusCode: http.StatusOK,
162168
request: &notionapi.DatabaseQueryRequest{
163169
Filter: &notionapi.PropertyFilter{
164170
Property: "Name",
@@ -190,7 +196,7 @@ func TestDatabaseClient(t *testing.T) {
190196

191197
for _, tt := range tests {
192198
t.Run(tt.name, func(t *testing.T) {
193-
c := newMockedClient(t, tt.filePath)
199+
c := newMockedClient(t, tt.filePath, tt.statusCode)
194200
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
195201
got, err := client.Database.Query(context.Background(), tt.id, tt.request)
196202

error.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package notionapi
2+
3+
type ErrorCode string
4+
5+
type Error struct {
6+
Object ObjectType `json:"object"`
7+
Status int `json:"status"`
8+
Code ErrorCode `json:"code"`
9+
Message string `json:"message"`
10+
}
11+
12+
func (e *Error) Error() string {
13+
return e.Message
14+
}

0 commit comments

Comments
 (0)