Skip to content

Commit 3cb69c1

Browse files
author
james_tsai
committed
feat: handle axios response error in interceptor
1 parent 3c0a7fd commit 3cb69c1

2 files changed

Lines changed: 72 additions & 26 deletions

File tree

nodejs/src/error.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class HackMDError extends Error {
2+
constructor(...args: any) {
3+
super(...args);
4+
Object.setPrototypeOf(this, new.target.prototype);
5+
}
6+
}
7+
8+
9+
class HttpResponseError extends HackMDError {
10+
public constructor(
11+
message: string,
12+
readonly code: number,
13+
readonly statusText: string,
14+
) {
15+
super(message);
16+
}
17+
}
18+
19+
class MissingRequiredArgument extends HackMDError {}
20+
class InternalServerError extends HttpResponseError {}
21+
22+
23+
export {
24+
HackMDError,
25+
HttpResponseError,
26+
MissingRequiredArgument,
27+
InternalServerError
28+
}

nodejs/src/index.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
1-
import axios, { AxiosInstance, AxiosRequestConfig, AxiosError } from 'axios'
2-
import { User, Note, Team, CreateNoteOptions, NotePermissionRole, CommentPermissionType } from './type'
1+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
2+
import { User, Note, Team, CreateNoteOptions } from './type'
3+
import * as HackMDErrors from './error'
34

45
export class API {
56
private axios: AxiosInstance
67

7-
constructor(public hackmdAPIEndpointURL: string, readonly accessToken: string) {
8+
constructor(public hackmdAPIEndpointURL:string, readonly accessToken: string) {
9+
if (!accessToken) {
10+
throw new HackMDErrors.MissingRequiredArgument('Missing access token when creating HackMD client')
11+
}
12+
813
this.axios = axios.create({
9-
baseURL: this.hackmdAPIEndpointURL,
14+
baseURL: hackmdAPIEndpointURL,
1015
headers:{
1116
"Content-Type": "application/json",
1217
}
1318
})
1419

1520
this.axios.interceptors.request.use(
1621
(config: AxiosRequestConfig) =>{
17-
if (accessToken) {
18-
config.headers!.Authorization = `Bearer ${this.accessToken}`;
19-
}
22+
config.headers!.Authorization = `Bearer ${accessToken}`;
2023
return config
2124
},
2225
(err: AxiosError) => {
23-
console.log('request error')
2426
return Promise.reject(err)
2527
}
2628
)
29+
30+
this.axios.interceptors.response.use(
31+
(response: AxiosResponse) => {
32+
return response
33+
},
34+
async (err: AxiosError) => {
35+
if (!err.response) {
36+
return Promise.reject(err);
37+
}
38+
39+
if (err.response.status >= 500) {
40+
throw new HackMDErrors.HttpResponseError(
41+
`HackMD internal error (${err.response.status} ${err.response.statusText})`,
42+
err.response.status,
43+
err.response.statusText,
44+
)
45+
} else {
46+
throw new HackMDErrors.HttpResponseError(
47+
`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`,
48+
err.response.status,
49+
err.response.statusText,
50+
)
51+
}
52+
}
53+
)
2754
}
2855

2956
getMe = async () => {
@@ -42,17 +69,12 @@ export class API {
4269
}
4370

4471
getNote = async (noteId: string) => {
45-
try {
46-
const { data } = await this.axios.get<Note>(`notes/${noteId}`)
47-
return data
48-
} catch (e) {
49-
console.log(e)
50-
}
72+
const { data } = await this.axios.get<Note>(`notes/${noteId}`)
73+
return data
5174
}
5275

5376
createNote = async (options: CreateNoteOptions) => {
5477
const { data } = await this.axios.post<Note>("notes", options)
55-
console.log(data)
5678
return data
5779
}
5880

@@ -62,11 +84,8 @@ export class API {
6284
}
6385

6486
deleteNote = async (noteId: string) => {
65-
try {
66-
const { data } = await this.axios.delete<void>(`notes/${noteId}`)
67-
return data
68-
} catch(e) {
69-
}
87+
const { data } = await this.axios.delete<void>(`notes/${noteId}`)
88+
return data
7089
}
7190

7291
getTeams = async () => {
@@ -90,12 +109,11 @@ export class API {
90109
}
91110

92111
deleteTeamNote = async (teamPath: string, noteId: string) => {
93-
try {
94-
const { data } = await this.axios.delete<void>(`teams/${teamPath}/notes/${noteId}`)
95-
console.log(data)
96-
return data
97-
} catch(e) {
98-
}
112+
const { data } = await this.axios.delete<void>(`teams/${teamPath}/notes/${noteId}`)
113+
return data
99114
}
100115
}
101116

117+
process.on('unhandledRejection', error => {
118+
console.log('unhandledRejection', error)
119+
})

0 commit comments

Comments
 (0)