Skip to content

Commit 833802f

Browse files
authored
エラーメッセージをわかりやすくする (#41)
* response.okがfalseの時にPromise.rejectするように変更 * コメントを追加 * テストケースを追加
1 parent c47675a commit 833802f

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

src/createClient.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,23 @@ export const createClient = ({
6969
});
7070

7171
if (!response.ok) {
72-
throw new Error(`fetch API response status: ${response.status}`);
72+
const message = await (async () => {
73+
// Enclose `response.json()` in a try since it may throw an error
74+
// Only return the `message` if there is a `message`
75+
try {
76+
const { message } = await response.json();
77+
return message ?? null;
78+
} catch (_) {
79+
return null;
80+
}
81+
})();
82+
return Promise.reject(
83+
new Error(
84+
`fetch API response status: ${response.status}${
85+
message ? `\n message is \`${message}\`` : ''
86+
}`
87+
)
88+
);
7389
}
7490

7591
if (method === 'DELETE') return;
@@ -84,9 +100,7 @@ export const createClient = ({
84100
throw error.response.data;
85101
}
86102

87-
return Promise.reject(
88-
new Error(`serviceDomain or endpoint may be wrong.\n Details: ${error}`)
89-
);
103+
return Promise.reject(new Error(`Network Error.\n Details: ${error}`));
90104
}
91105
};
92106

tests/createClient.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { rest } from 'msw';
12
import { createClient } from '../src/createClient';
3+
import { testBaseUrl } from './mocks/handlers';
4+
import { server } from './mocks/server';
25

36
describe('createClient', () => {
47
test('Functions is generated to request the API', () => {
@@ -41,4 +44,60 @@ describe('createClient', () => {
4144
createClient({ serviceDomain: 'foo', apiKey: 10 })
4245
).toThrowError(new Error('parameter is not string'));
4346
});
47+
48+
describe('Throws an error when response.ok is false', () => {
49+
test('If there is a message', () => {
50+
server.use(
51+
rest.get(`${testBaseUrl}/list-type`, async (_, res, ctx) => {
52+
return res(
53+
ctx.status(401),
54+
ctx.json({ message: 'X-MICROCMS-KEY header is invalid.' })
55+
);
56+
})
57+
);
58+
const client = createClient({
59+
serviceDomain: 'serviceDomain',
60+
apiKey: 'apiKey',
61+
});
62+
63+
expect(client.get({ endpoint: 'list-type' })).rejects.toThrowError(
64+
new Error(
65+
'fetch API response status: 401\n message is `X-MICROCMS-KEY header is invalid.`'
66+
)
67+
);
68+
});
69+
test('If there is no message', () => {
70+
server.use(
71+
rest.get(`${testBaseUrl}/list-type`, async (_, res, ctx) => {
72+
return res(ctx.status(404));
73+
})
74+
);
75+
const client = createClient({
76+
serviceDomain: 'serviceDomain',
77+
apiKey: 'apiKey',
78+
});
79+
80+
expect(client.get({ endpoint: 'list-type' })).rejects.toThrowError(
81+
new Error('fetch API response status: 404')
82+
);
83+
});
84+
});
85+
86+
test('Throws an error in the event of a network error.', () => {
87+
server.use(
88+
rest.get(`${testBaseUrl}/list-type`, async (_, res) => {
89+
return res.networkError('Failed to fetch');
90+
})
91+
);
92+
const client = createClient({
93+
serviceDomain: 'serviceDomain',
94+
apiKey: 'apiKey',
95+
});
96+
97+
expect(client.get({ endpoint: 'list-type' })).rejects.toThrowError(
98+
new Error(
99+
'Network Error.\n Details: FetchError: request to https://servicedomain.microcms.io/api/v1/list-type failed, reason: Failed to fetch'
100+
)
101+
);
102+
});
44103
});

tests/get.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('get', () => {
6868

6969
expect(client.get({ endpoint: 'list-type' })).rejects.toThrow(
7070
new Error(
71-
'serviceDomain or endpoint may be wrong.\n Details: Error: fetch API response status: 500'
71+
'fetch API response status: 500\n message is `Internal Server Error`'
7272
)
7373
);
7474
});

0 commit comments

Comments
 (0)