Skip to content

Commit

Permalink
Add api error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jomei committed May 30, 2021
1 parent 6e77466 commit 2e0d366
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 115 deletions.
49 changes: 27 additions & 22 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notionapi_test
import (
"context"
"github.com/jomei/notionapi"
"net/http"
"reflect"
"testing"
"time"
Expand All @@ -15,24 +16,26 @@ func TestBlockClient(t *testing.T) {
}
t.Run("GetChildren", func(t *testing.T) {
tests := []struct {
name string
filePath string
id notionapi.BlockID
len int
wantErr bool
err error
name string
filePath string
statusCode int
id notionapi.BlockID
len int
wantErr bool
err error
}{
{
name: "returns blocks by id of parent block",
id: "some_id",
filePath: "testdata/block_get_children.json",
len: 2,
name: "returns blocks by id of parent block",
id: "some_id",
statusCode: http.StatusOK,
filePath: "testdata/block_get_children.json",
len: 2,
},
}

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

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

t.Run("AppendChildren", func(t *testing.T) {
tests := []struct {
name string
filePath string
id notionapi.BlockID
request *notionapi.AppendBlockChildrenRequest
want *notionapi.ChildPageBlock
wantErr bool
err error
name string
filePath string
statusCode int
id notionapi.BlockID
request *notionapi.AppendBlockChildrenRequest
want *notionapi.ChildPageBlock
wantErr bool
err error
}{
{
name: "returns blocks by id of parent block",
id: "some_id",
filePath: "testdata/block_append_children.json",
name: "returns blocks by id of parent block",
id: "some_id",
filePath: "testdata/block_append_children.json",
statusCode: http.StatusOK,
request: &notionapi.AppendBlockChildrenRequest{
Children: []notionapi.Block{
&notionapi.Heading2Block{
Expand Down Expand Up @@ -96,7 +101,7 @@ func TestBlockClient(t *testing.T) {

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

Expand Down
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -119,7 +118,13 @@ func (c *Client) request(ctx context.Context, method string, urlStr string, quer
}

if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("http status: %d", res.StatusCode)
var apiErr Error
err = json.NewDecoder(res.Body).Decode(&apiErr)
if err != nil {
return nil, err
}

return nil, &apiErr
}

return res, nil
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func newTestClient(fn RoundTripFunc) *http.Client {
}

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

resp := &http.Response{
StatusCode: http.StatusOK,
StatusCode: statusCode,
Body: b,
Header: make(http.Header),
}
Expand Down
1 change: 1 addition & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
ObjectTypeList ObjectType = "list"
ObjectTypeText ObjectType = "text"
ObjectTypeUser ObjectType = "user"
ObjectTypeError ObjectType = "error"
)

const (
Expand Down
66 changes: 36 additions & 30 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notionapi_test
import (
"context"
"github.com/jomei/notionapi"
"net/http"
"reflect"
"testing"
"time"
Expand All @@ -16,17 +17,19 @@ func TestDatabaseClient(t *testing.T) {

t.Run("Get", func(t *testing.T) {
tests := []struct {
name string
filePath string
id notionapi.DatabaseID
want *notionapi.Database
wantErr bool
err error
name string
filePath string
statusCode int
id notionapi.DatabaseID
want *notionapi.Database
wantErr bool
err error
}{
{
name: "returns database by id",
id: "some_id",
filePath: "testdata/database_get.json",
name: "returns database by id",
id: "some_id",
filePath: "testdata/database_get.json",
statusCode: http.StatusOK,
want: &notionapi.Database{
Object: notionapi.ObjectTypeDatabase,
ID: "some_id",
Expand Down Expand Up @@ -69,8 +72,7 @@ func TestDatabaseClient(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

c := newMockedClient(t, tt.filePath)
c := newMockedClient(t, tt.filePath, tt.statusCode)
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
got, err := client.Database.Get(context.Background(), tt.id)

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

t.Run("List", func(t *testing.T) {
tests := []struct {
name string
filePath string
want *notionapi.DatabaseListResponse
wantErr bool
err error
name string
filePath string
statusCode int
want *notionapi.DatabaseListResponse
wantErr bool
err error
}{
{
name: "returns list of databases",
filePath: "testdata/database_list.json",
name: "returns list of databases",
filePath: "testdata/database_list.json",
statusCode: http.StatusOK,
want: &notionapi.DatabaseListResponse{
Object: notionapi.ObjectTypeList,
Results: []notionapi.Database{
Expand Down Expand Up @@ -128,7 +132,7 @@ func TestDatabaseClient(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newMockedClient(t, tt.filePath)
c := newMockedClient(t, tt.filePath, tt.statusCode)
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))

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

t.Run("Query", func(t *testing.T) {
tests := []struct {
name string
filePath string
id notionapi.DatabaseID
request *notionapi.DatabaseQueryRequest
want *notionapi.DatabaseQueryResponse
wantErr bool
err error
name string
filePath string
statusCode int
id notionapi.DatabaseID
request *notionapi.DatabaseQueryRequest
want *notionapi.DatabaseQueryResponse
wantErr bool
err error
}{
{
name: "returns query results",
id: "some_id",
filePath: "testdata/database_query.json",
name: "returns query results",
id: "some_id",
filePath: "testdata/database_query.json",
statusCode: http.StatusOK,
request: &notionapi.DatabaseQueryRequest{
Filter: &notionapi.PropertyFilter{
Property: "Name",
Expand Down Expand Up @@ -190,7 +196,7 @@ func TestDatabaseClient(t *testing.T) {

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

Expand Down
14 changes: 14 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package notionapi

type ErrorCode string

type Error struct {
Object ObjectType `json:"object"`
Status int `json:"status"`
Code ErrorCode `json:"code"`
Message string `json:"message"`
}

func (e *Error) Error() string {
return e.Message
}
Loading

0 comments on commit 2e0d366

Please sign in to comment.