mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-11-26 06:11:14 +00:00
Better error wrapping. (#126)
* Better error wrapping. * Simplifying * Moving return to else.
This commit is contained in:
parent
c4c97dcccd
commit
d886904888
2 changed files with 10 additions and 25 deletions
13
package.json
13
package.json
|
@ -7,9 +7,7 @@
|
||||||
"author": "Dessalines <tyhou13@gmx.com>",
|
"author": "Dessalines <tyhou13@gmx.com>",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"files": [
|
"files": ["/dist"],
|
||||||
"/dist"
|
|
||||||
],
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"docs": "typedoc src/index.ts",
|
"docs": "typedoc src/index.ts",
|
||||||
|
@ -17,13 +15,8 @@
|
||||||
"prepare": "yarn run build && husky install"
|
"prepare": "yarn run build && husky install"
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"*.{ts,tsx,js}": [
|
"*.{ts,tsx,js}": ["prettier --write", "eslint --fix"],
|
||||||
"prettier --write",
|
"package.json": ["sortpack"]
|
||||||
"eslint --fix"
|
|
||||||
],
|
|
||||||
"package.json": [
|
|
||||||
"sortpack"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cross-fetch": "^3.1.5",
|
"cross-fetch": "^3.1.5",
|
||||||
|
|
22
src/http.ts
22
src/http.ts
|
@ -1295,18 +1295,15 @@ export class LemmyHttp {
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
form: BodyType
|
form: BodyType
|
||||||
): Promise<ResponseType> {
|
): Promise<ResponseType> {
|
||||||
|
let response: Response;
|
||||||
if (type_ === HttpType.Get) {
|
if (type_ === HttpType.Get) {
|
||||||
const getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
const getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
||||||
const response = await fetch(getUrl, {
|
response = await fetch(getUrl, {
|
||||||
method: HttpType.Get,
|
method: HttpType.Get,
|
||||||
headers: this.headers,
|
headers: this.headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.checkandThrowError(response);
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
} else {
|
} else {
|
||||||
const response = await fetch(this.buildFullUrl(endpoint), {
|
response = await fetch(this.buildFullUrl(endpoint), {
|
||||||
method: type_,
|
method: type_,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
@ -1314,18 +1311,13 @@ export class LemmyHttp {
|
||||||
},
|
},
|
||||||
body: JSON.stringify(form),
|
body: JSON.stringify(form),
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.checkandThrowError(response);
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const json = await response.json();
|
||||||
|
|
||||||
private async checkandThrowError(response: Response) {
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errJson = await response.json();
|
throw json["error"] ?? response.statusText;
|
||||||
const errString: string = errJson["error"] ?? response.statusText;
|
} else {
|
||||||
throw new Error(errString);
|
return json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue