mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
convert error log from service to saga
This commit is contained in:
committed by
David Lechner
parent
17cb5a32d5
commit
292f2d83d2
@@ -0,0 +1,45 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
|
||||
import { AsyncSaga } from '../../test';
|
||||
import { didFailToWrite } from '../actions/ble';
|
||||
import {
|
||||
BootloaderConnectionFailureReason,
|
||||
didError,
|
||||
didFailToConnect,
|
||||
} from '../actions/lwp3-bootloader';
|
||||
import errorLog from './error-log';
|
||||
|
||||
test('bleDataDidFailToWrite', async () => {
|
||||
const saga = new AsyncSaga(errorLog);
|
||||
|
||||
console.error = jest.fn();
|
||||
saga.put(didFailToWrite(0, new Error('test error')));
|
||||
expect(console.error).toHaveBeenCalledTimes(1);
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
|
||||
test('bootloaderDidFailToConnect', async () => {
|
||||
const saga = new AsyncSaga(errorLog);
|
||||
|
||||
console.debug = jest.fn();
|
||||
saga.put(didFailToConnect(BootloaderConnectionFailureReason.Canceled));
|
||||
expect(console.debug).toHaveBeenCalledTimes(1);
|
||||
|
||||
console.error = jest.fn();
|
||||
saga.put(didFailToConnect(BootloaderConnectionFailureReason.Unknown));
|
||||
expect(console.error).toHaveBeenCalledTimes(1);
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
|
||||
test('bootloaderDidError', async () => {
|
||||
const saga = new AsyncSaga(errorLog);
|
||||
|
||||
console.error = jest.fn();
|
||||
saga.put(didError(new Error('test error')));
|
||||
expect(console.error).toHaveBeenCalledTimes(1);
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
|
||||
import { takeEvery } from 'redux-saga/effects';
|
||||
import { BLEDataActionType, BLEDataDidFailToWriteAction } from '../actions/ble';
|
||||
import {
|
||||
BootloaderConnectionActionType,
|
||||
BootloaderConnectionDidErrorAction,
|
||||
BootloaderConnectionDidFailToConnectAction,
|
||||
BootloaderConnectionFailureReason,
|
||||
} from '../actions/lwp3-bootloader';
|
||||
|
||||
function bleDataDidFailToWrite(action: BLEDataDidFailToWriteAction): void {
|
||||
console.error(action.err);
|
||||
}
|
||||
|
||||
function bootloaderDidFailToConnect(
|
||||
action: BootloaderConnectionDidFailToConnectAction,
|
||||
): void {
|
||||
if (action.reason === BootloaderConnectionFailureReason.Unknown) {
|
||||
console.error(action.err);
|
||||
} else {
|
||||
console.debug(action.err);
|
||||
}
|
||||
}
|
||||
|
||||
function bootloaderDidError(action: BootloaderConnectionDidErrorAction): void {
|
||||
console.error(action.err);
|
||||
}
|
||||
|
||||
export default function* (): Generator {
|
||||
yield takeEvery(BLEDataActionType.DidFailToWrite, bleDataDidFailToWrite);
|
||||
yield takeEvery(
|
||||
BootloaderConnectionActionType.DidFailToConnect,
|
||||
bootloaderDidFailToConnect,
|
||||
);
|
||||
yield takeEvery(BootloaderConnectionActionType.DidError, bootloaderDidError);
|
||||
}
|
||||
+2
-1
@@ -3,6 +3,7 @@
|
||||
|
||||
import { all } from 'redux-saga/effects';
|
||||
import editor from './editor';
|
||||
import errorLog from './error-log';
|
||||
import flashFirmware from './flash-firmare';
|
||||
import bootloader from './lwp3-bootloader';
|
||||
import mpy from './mpy';
|
||||
@@ -10,5 +11,5 @@ import terminal from './terminal';
|
||||
|
||||
/* istanbul ignore next */
|
||||
export default function* (): Generator {
|
||||
yield all([bootloader(), editor(), flashFirmware(), mpy(), terminal()]);
|
||||
yield all([bootloader(), editor(), errorLog(), flashFirmware(), mpy(), terminal()]);
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
|
||||
import { Action } from '../actions';
|
||||
import { BLEDataActionType } from '../actions/ble';
|
||||
import {
|
||||
BootloaderConnectionActionType,
|
||||
BootloaderConnectionFailureReason,
|
||||
} from '../actions/lwp3-bootloader';
|
||||
import { combineServices } from '.';
|
||||
|
||||
/**
|
||||
* Logs unexpected errors to console.error and expected errors to console.debug.
|
||||
* @param action An action
|
||||
*/
|
||||
function consoleLog(action: Action): void {
|
||||
switch (action.type) {
|
||||
case BLEDataActionType.DidFailToWrite:
|
||||
console.error(action.err);
|
||||
break;
|
||||
case BootloaderConnectionActionType.DidFailToConnect:
|
||||
if (action.reason === BootloaderConnectionFailureReason.Unknown) {
|
||||
console.error(action.err);
|
||||
} else {
|
||||
console.debug(action.err);
|
||||
}
|
||||
break;
|
||||
case BootloaderConnectionActionType.DidError:
|
||||
console.error(action.err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export default combineServices(consoleLog);
|
||||
@@ -5,7 +5,6 @@ import { Middleware } from 'redux';
|
||||
import { Action, Dispatch } from '../actions';
|
||||
import { RootState } from '../reducers';
|
||||
import ble from './ble';
|
||||
import errorLog from './error-log';
|
||||
import hub from './hub';
|
||||
import bootloader from './lwp3-bootloader';
|
||||
|
||||
@@ -38,7 +37,7 @@ export function combineServices(...services: Service[]): Service {
|
||||
};
|
||||
}
|
||||
|
||||
const rootService = combineServices(ble, bootloader, errorLog, hub);
|
||||
const rootService = combineServices(ble, bootloader, hub);
|
||||
|
||||
const serviceMiddleware: Middleware = (store) => (next) => (action): unknown => {
|
||||
runService(rootService, action, store.dispatch, store.getState());
|
||||
|
||||
Reference in New Issue
Block a user