Update generate failed and set status code

This commit is contained in:
Sambo Chea 2022-11-30 11:12:40 +07:00
parent 2996a6056e
commit d54b69ce80
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
2 changed files with 20 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@cubetiq/hlt", "name": "@cubetiq/hlt",
"version": "0.1.2", "version": "0.1.3",
"description": "A lightweight http tunnel client using nodejs and socket.io client", "description": "A lightweight http tunnel client using nodejs and socket.io client",
"main": "dist/client.js", "main": "dist/client.js",
"bin": { "bin": {

View File

@ -265,6 +265,7 @@ program
config.apiKey = options.key; config.apiKey = options.key;
} }
let errorCode = 0;
if (!config.token || options.force) { if (!config.token || options.force) {
console.log("Generating token..."); console.log("Generating token...");
await getTokenFree(config.server, { await getTokenFree(config.server, {
@ -273,22 +274,26 @@ program
apiKey: config.apiKey, apiKey: config.apiKey,
}) })
.then((resp: any) => { .then((resp: any) => {
console.log("Token generated successfully!");
if (resp.data?.token) { if (resp.data?.token) {
console.log("Token generated successfully!");
config.token = resp.data?.token; config.token = resp.data?.token;
} else { } else {
console.log("free token return with null or empty from server"); errorCode = 1;
console.error("Generate free token failed, return with null or empty from server!", resp);
return; return;
} }
}) })
.catch((err: any) => { .catch((err: any) => {
errorCode = 1;
console.error("cannot get free token from server", err); console.error("cannot get free token from server", err);
return; return;
}); });
} }
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2)); if (errorCode === 0) {
console.log(`initialized config saved successfully to: ${configFilePath}`); fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
console.log(`initialized config saved successfully to: ${configFilePath}`);
}
}); });
// start // start
@ -407,6 +412,9 @@ program
config.server = SERVER_DEFAULT_URL; config.server = SERVER_DEFAULT_URL;
} }
// Error Code status
let errorCode = 0;
if (type === "token" || type === "jwt") { if (type === "token" || type === "jwt") {
config.token = value; config.token = value;
} else if (type === "server") { } else if (type === "server") {
@ -430,11 +438,13 @@ program
if (resp.data?.token) { if (resp.data?.token) {
config.token = resp.data?.token; config.token = resp.data?.token;
} else { } else {
console.log("free token return with null or empty from server"); errorCode = 1;
console.error("Generate free token failed, return with null or empty from server!", resp);
return; return;
} }
}) })
.catch((err: any) => { .catch((err: any) => {
errorCode = 1;
console.error("cannot get free token from server", err); console.error("cannot get free token from server", err);
return; return;
}); });
@ -445,8 +455,10 @@ program
config.clientId = config.apiKey; config.clientId = config.apiKey;
} }
fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2)); if (errorCode === 0) {
console.log(`${type} config saved successfully to: ${configFilePath}`); fs.writeFileSync(configFilePath, JSON.stringify(config, null, 2));
console.log(`${type} config saved successfully to: ${configFilePath}`);
}
}); });
// config // config