mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-17 14:30:34 +08:00
feat(cli): Log cli errors to lowdefy api.
This commit is contained in:
parent
2ce90d1b5c
commit
4e8ef7b346
@ -14,7 +14,37 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import createPrint from './print';
|
||||
import packageJson from '../../package.json';
|
||||
|
||||
const { version: cliVersion } = packageJson;
|
||||
|
||||
/* TODO:
|
||||
- log lowdefy version
|
||||
- log command
|
||||
- respect disable telemetry
|
||||
*/
|
||||
async function logError(error) {
|
||||
try {
|
||||
await axios.request({
|
||||
method: 'post',
|
||||
url: 'https://api.lowdefy.net/errors',
|
||||
headers: {
|
||||
'User-Agent': `Lowdefy CLI v${cliVersion}`,
|
||||
},
|
||||
data: {
|
||||
source: 'cli',
|
||||
cliVersion,
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
stack: error.stack,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
// pass
|
||||
}
|
||||
}
|
||||
|
||||
function errorHandler(fn, options = {}) {
|
||||
async function run(...args) {
|
||||
@ -24,6 +54,7 @@ function errorHandler(fn, options = {}) {
|
||||
} catch (error) {
|
||||
const print = createPrint();
|
||||
print.error(error.message);
|
||||
await logError(error);
|
||||
// TODO: Stay alive feature
|
||||
}
|
||||
}
|
||||
|
@ -14,8 +14,13 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import errorHandler from './errorHandler';
|
||||
import createPrint from './print';
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
import packageJson from '../../package.json';
|
||||
|
||||
jest.mock('../../package.json', () => ({ version: 'cliVersion' }));
|
||||
|
||||
jest.mock('./print', () => {
|
||||
const error = jest.fn();
|
||||
@ -23,6 +28,7 @@ jest.mock('./print', () => {
|
||||
error,
|
||||
});
|
||||
});
|
||||
jest.mock('axios');
|
||||
|
||||
const print = createPrint();
|
||||
|
||||
@ -70,6 +76,17 @@ test('Catch error synchronous function', async () => {
|
||||
await wrapped();
|
||||
expect(fn).toHaveBeenCalled();
|
||||
expect(print.error.mock.calls).toEqual([['Error']]);
|
||||
const axiosAgruments = axios.request.mock.calls[0][0];
|
||||
expect(axiosAgruments.headers).toEqual({
|
||||
'User-Agent': 'Lowdefy CLI vcliVersion',
|
||||
});
|
||||
expect(axiosAgruments.url).toEqual('https://api.lowdefy.net/errors');
|
||||
expect(axiosAgruments.method).toEqual('post');
|
||||
expect(axiosAgruments.data.cliVersion).toEqual('cliVersion');
|
||||
expect(axiosAgruments.data.message).toEqual('Error');
|
||||
expect(axiosAgruments.data.name).toEqual('Error');
|
||||
expect(axiosAgruments.data.source).toEqual('cli');
|
||||
expect(axiosAgruments.data.stack).toMatch('Error: Error');
|
||||
});
|
||||
|
||||
test('Catch error asynchronous function', async () => {
|
||||
@ -81,6 +98,17 @@ test('Catch error asynchronous function', async () => {
|
||||
await wrapped();
|
||||
expect(fn).toHaveBeenCalled();
|
||||
expect(print.error.mock.calls).toEqual([['Async Error']]);
|
||||
const axiosAgruments = axios.request.mock.calls[0][0];
|
||||
expect(axiosAgruments.headers).toEqual({
|
||||
'User-Agent': 'Lowdefy CLI vcliVersion',
|
||||
});
|
||||
expect(axiosAgruments.url).toEqual('https://api.lowdefy.net/errors');
|
||||
expect(axiosAgruments.method).toEqual('post');
|
||||
expect(axiosAgruments.data.cliVersion).toEqual('cliVersion');
|
||||
expect(axiosAgruments.data.message).toEqual('Async Error');
|
||||
expect(axiosAgruments.data.name).toEqual('Error');
|
||||
expect(axiosAgruments.data.source).toEqual('cli');
|
||||
expect(axiosAgruments.data.stack).toMatch('Error: Async Error');
|
||||
});
|
||||
|
||||
// test('Catch error synchronous function, stay alive', async () => {
|
||||
|
@ -25,7 +25,7 @@ function getSendTelemetry({ appId, cliVersion, disableTelemetry, lowdefyVersion,
|
||||
method: 'post',
|
||||
url: 'https://api.lowdefy.net/telemetry/cli',
|
||||
headers: {
|
||||
'User-Agent': `Lowdefy CLI v${lowdefyVersion}`,
|
||||
'User-Agent': `Lowdefy CLI v${cliVersion}`,
|
||||
},
|
||||
data: {
|
||||
...data,
|
||||
|
@ -57,7 +57,7 @@ test('send telemetry', async () => {
|
||||
x: 1,
|
||||
},
|
||||
headers: {
|
||||
'User-Agent': 'Lowdefy CLI vlowdefyVersion',
|
||||
'User-Agent': 'Lowdefy CLI vcliVersion',
|
||||
},
|
||||
method: 'post',
|
||||
url: 'https://api.lowdefy.net/telemetry/cli',
|
||||
|
Loading…
Reference in New Issue
Block a user