-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathapp.js
128 lines (100 loc) · 2.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
fetch('/user')
.then(res => res.json())
.then(user => console.log('do something', user));
// conver this code to promise based
/* const res = await fetch('/user');
const user = await res.json();
*/
// ERROR HANDLING
try {
const res = await fetch('/user');
const user = await res.json();
} catch (err) {
logAndHandlerError(err);
}
// OK AND CONTENT TYPE
//The response headers are available in a Map-like headers object in response.headers.
try {
const res = await fetch('/user');
if (!res.ok || res.headers.get('Content-Type') !== 'application/json') {
throw new Error('Unexpected status code or content type');
// 'Unexpected status code or content type'
}
const user = await res.json();
} catch (err) {
logAndHandleError(err);
}
// CUSTOME ERROR
class ResponseError extends Error {
constructor(message, response) {
super(message);
this.response = response;
}
}
try {
const res = await fetch('/user');
if (!res.ok || res.headers.get('Content-Type') !== 'application/json') {
throw new ResponseError('Unexpected status code or content type', response);
}
const user = await res.json();
} catch (err) {
function logAndHandleError(err) {
if (err instanceof ResponseError) {
// err.response?.status:
// 400
// 500
// 404
}
}
logAndHandleError(err);
}
// CAUSE
try {
const res = await fetch('/user');
if (!res.ok || res.headers.get('Content-Type') !== 'application/json') {
throw new Error('Unexpected status code or content type', {
cause: { res },
});
}
const user = await res.json();
} catch (err) {
function logAndHandleError(err) {
// err.cause.res?.status:
// 400
// 500
// 404
}
logAndHandleError(err);
}
// eliminar application/json
try {
const res = await fetch('/user');
if (!res.ok || res.headers.get('Content-Type') !== 'application/json') {
throw new Error('Unexpected status code or content type', {
cause: { res },
});
}
const user = await res.json();
} catch (err) {
function logAndHandleError(err) {
// err.cause.res?.status:
// 400
// 500
// 404
if (err instanceof SyntaxError) {
// surprise, there was no json
}
if (err.headers.get('Content-Type') !== 'application/json') {
}
}
logAndHandleError(err);
}
//AbortController
/* Response provides multiple promise-based methods to access the body in various formats:
response.text() – read the response and return as text,
response.json() – parse the response as JSON,
response.formData() – return the response as FormData object (explained in the next chapter),
response.blob() – return the response as Blob (binary data with type),
response.arrayBuffer() – return the response as ArrayBuffer (low-level representation of binary data),
additionally, response.body is a ReadableStream object, it allows you to read the body chunk-by-chunk, we’ll see an example later.
*/