Skip to content

Commit 8f0ca60

Browse files
committed
fix error message
1 parent 6fcdb98 commit 8f0ca60

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

src/api.ts

+18-25
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import packageJson from '../package.json';
77
import tcpp from 'tcp-ping';
88
import filesizeParser from 'filesize-parser';
99
import { pricingPageUrl } from './utils';
10-
import { Session } from 'types';
10+
import type { Session } from 'types';
1111
import FormData from 'form-data';
1212

1313
const tcpPing = util.promisify(tcpp.ping);
@@ -20,15 +20,13 @@ let host = process.env.PUSHY_REGISTRY || defaultEndpoint;
2020

2121
const userAgent = `react-native-update-cli/${packageJson.version}`;
2222

23-
export const getSession = function () {
24-
return session;
25-
};
23+
export const getSession = () => session;
2624

27-
export const replaceSession = function (newSession: { token: string }) {
25+
export const replaceSession = (newSession: { token: string }) => {
2826
session = newSession;
2927
};
3028

31-
export const loadSession = async function () {
29+
export const loadSession = async () => {
3230
if (fs.existsSync('.update')) {
3331
try {
3432
replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8')));
@@ -42,7 +40,7 @@ export const loadSession = async function () {
4240
}
4341
};
4442

45-
export const saveSession = function () {
43+
export const saveSession = () => {
4644
// Only save on change.
4745
if (session !== savedSession) {
4846
const current = session;
@@ -52,7 +50,7 @@ export const saveSession = function () {
5250
}
5351
};
5452

55-
export const closeSession = function () {
53+
export const closeSession = () => {
5654
if (fs.existsSync('.update')) {
5755
fs.unlinkSync('.update');
5856
savedSession = undefined;
@@ -64,38 +62,35 @@ export const closeSession = function () {
6462
async function query(url: string, options: fetch.RequestInit) {
6563
const resp = await fetch(url, options);
6664
const text = await resp.text();
67-
let json;
65+
let json: any;
6866
try {
6967
json = JSON.parse(text);
70-
} catch (e) {
71-
if (resp.statusText.includes('Unauthorized')) {
72-
throw new Error('登录信息已过期,请使用 pushy login 命令重新登录');
73-
} else {
74-
throw new Error(`Server error: ${resp.statusText}`);
75-
}
76-
}
68+
} catch (e) {}
7769

7870
if (resp.status !== 200) {
79-
throw new Error(`${resp.status}: ${resp.statusText}`);
71+
const message = json?.message || resp.statusText;
72+
if (resp.status === 401) {
73+
throw new Error('登录信息已过期,请使用 pushy login 命令重新登录');
74+
}
75+
throw new Error(message);
8076
}
8177
return json;
8278
}
8379

8480
function queryWithoutBody(method: string) {
85-
return function (api: string) {
86-
return query(host + api, {
81+
return (api: string) =>
82+
query(host + api, {
8783
method,
8884
headers: {
8985
'User-Agent': userAgent,
9086
'X-AccessToken': session ? session.token : '',
9187
},
9288
});
93-
};
9489
}
9590

9691
function queryWithBody(method: string) {
97-
return function (api: string, body: Record<string, any>) {
98-
return query(host + api, {
92+
return (api: string, body: Record<string, any>) =>
93+
query(host + api, {
9994
method,
10095
headers: {
10196
'User-Agent': userAgent,
@@ -104,10 +99,8 @@ function queryWithBody(method: string) {
10499
},
105100
body: JSON.stringify(body),
106101
});
107-
};
108102
}
109103

110-
export const get = queryWithoutBody('GET');
111104
export const post = queryWithBody('POST');
112105
export const put = queryWithBody('PUT');
113106
export const doDelete = queryWithBody('DELETE');
@@ -155,7 +148,7 @@ export async function uploadFile(fn: string, key?: string) {
155148
form.append(k, v);
156149
});
157150
const fileStream = fs.createReadStream(fn);
158-
fileStream.on('data', function (data) {
151+
fileStream.on('data', (data) => {
159152
bar.tick(data.length);
160153
});
161154

src/app.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { question } from './utils';
2-
import fs from 'fs';
2+
import fs from 'node:fs';
33
import Table from 'tty-table';
44

55
import { post, get, doDelete } from './api';
@@ -84,7 +84,7 @@ export const commands = {
8484
options: { platform, downloadUrl },
8585
});
8686
},
87-
deleteApp: async function ({ args, options }) {
87+
deleteApp: async ({ args, options }) => {
8888
const { platform } = options;
8989
const id = args[0] || chooseApp(platform);
9090
if (!id) {
@@ -93,15 +93,17 @@ export const commands = {
9393
await doDelete(`/app/${id}`);
9494
console.log('操作成功');
9595
},
96-
apps: async function ({ options }) {
96+
apps: async ({ options }) => {
9797
const { platform } = options;
9898
listApp(platform);
9999
},
100-
selectApp: async function ({ args, options }) {
100+
selectApp: async ({ args, options }) => {
101101
const platform = checkPlatform(
102102
options.platform || (await question('平台(ios/android/harmony):')),
103103
);
104-
const id = args[0] ? parseInt(args[0]) : (await chooseApp(platform)).id;
104+
const id = args[0]
105+
? Number.parseInt(args[0])
106+
: (await chooseApp(platform)).id;
105107

106108
let updateInfo = {};
107109
if (fs.existsSync('update.json')) {

src/user.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { question } from './utils';
22
import { post, get, replaceSession, saveSession, closeSession } from './api';
3-
import crypto from 'crypto';
3+
import crypto from 'node:crypto';
44

55
function md5(str) {
66
return crypto.createHash('md5').update(str).digest('hex');
77
}
88

99
export const commands = {
10-
login: async function ({ args }) {
10+
login: async ({ args }) => {
1111
const email = args[0] || (await question('email:'));
1212
const pwd = args[1] || (await question('password:', true));
1313
const { token, info } = await post('/user/login', {
@@ -18,11 +18,11 @@ export const commands = {
1818
await saveSession();
1919
console.log(`欢迎使用 pushy 热更新服务, ${info.name}.`);
2020
},
21-
logout: async function () {
21+
logout: async () => {
2222
await closeSession();
2323
console.log('已退出登录');
2424
},
25-
me: async function () {
25+
me: async () => {
2626
const me = await get('/user/me');
2727
for (const k in me) {
2828
if (k !== 'ok') {

0 commit comments

Comments
 (0)