Passed
Push — master ( 497146...85c7ba )
by Zhenyu
02:16
created

parser.js ➔ ???   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 2
dl 0
loc 21
rs 10
nop 1
1
import { isFetchResponseError, isFetchNetworkError } from './checker';
2
import nError from './creator';
3
import { CATEGORIES } from './constants';
4
5
// parse the response error based on content-type text/html, text/plain or application/json
6
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
7
// https://msdn.microsoft.com/en-us/library/ms526971(v=exchg.10).aspx
8
export const parseFetchResponseError = async response => {
0 ignored issues
show
Bug introduced by
The variable async seems to be never declared. If this is a global, consider adding a /** global: async */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
	if (response.ok) {
10
		return nError({
11
			category: CATEGORIES.FETCH_RESPONSE_OK,
12
			status: response.status,
13
			message: "it shouldn't be caught as exception, please check the code",
14
		});
15
	}
16
17
	const { status, headers } = response;
18
	const contentType = headers.get('content-type');
19
	const parseMethod =
20
		contentType && contentType.includes('application/json') ? 'json' : 'text';
21
	const message = await response[parseMethod](); // system Error would be thrown if it fails
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
22
	return nError({
23
		category: CATEGORIES.FETCH_RESPONSE_ERROR,
24
		status,
25
		message,
26
		contentType,
27
	});
28
};
29
30
export const parseFetchNetworkError = e =>
31
	nError({
32
		category: CATEGORIES.FETCH_NETWORK_ERROR,
33
		message: e.message,
34
		code: e.code,
35
	});
36
37
export const parseFetchError = async e => {
38
	if (isFetchResponseError(e)) {
39
		const parsedError = await parseFetchResponseError(e);
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
		return parsedError;
41
	}
42
	if (isFetchNetworkError(e)) {
43
		return parseFetchNetworkError(e);
44
	}
45
	return e; // uncaught exception
46
};
47