Skip to content

Commit

Permalink
Fix #22. Improve error handling (#27)
Browse files Browse the repository at this point in the history
* Fix pause method referrence in loaded latency engine

* Expose onError method

* Add error logging to example

* Bump dev deps
  • Loading branch information
vasturiano committed Sep 11, 2023
1 parent ae201d4 commit 3b720e3
Show file tree
Hide file tree
Showing 7 changed files with 1,009 additions and 829 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ new SpeedTest({ configOptions })
| <b>restart()</b> | Clears the current results and restarts the measurements from the beginning. |

### Notification Events
| Event Method | Arguments | Description |
| --- | --- | :--: |
| <b>onRunningChange</b> | running: <i>boolean</i> | Invoked whenever the test engine starts or stops. The current state is included as a function argument. |
| <b>onResultsChange</b> | { type: <i>string</i> } | Invoked whenever any item changes in the results, usually indicating the completion of a measurement. The type of measurement that changed is included as an info attribute in the function argument. |
| <b>onFinish</b> | results: <i>[Results](#results-object)</i> | Invoked whenever the test engine finishes all the measurements. The final [results object](#results-object) is included as a function argument. |
| Event Method | Arguments | Description |
|------------------------|--------------------------------------------| :--: |
| <b>onRunningChange</b> | running: <i>boolean</i> | Invoked whenever the test engine starts or stops. The current state is included as a function argument. |
| <b>onResultsChange</b> | { type: <i>string</i> } | Invoked whenever any item changes in the results, usually indicating the completion of a measurement. The type of measurement that changed is included as an info attribute in the function argument. |
| <b>onFinish</b> | results: <i>[Results](#results-object)</i> | Invoked whenever the test engine finishes all the measurements. The final [results object](#results-object) is included as a function argument. |
| <b>onError</b> | error: <i>string</i> | Invoked whenever an error occurs during one of the measurements. The error details are included as a function argument. |

### Measurement config
The specific measurements to be performed by the test engine (and their sequence) can be customized using the `measurements` config option. This should be an array of objects, each with a `type` field, plus additional fields specific to that measurement type.
Expand Down
2 changes: 2 additions & 0 deletions example/basic-page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
console.log(results.getScores());
};

engine.onError = (e) => console.log(e);

const playButton = document.createElement('button');
playButton.textContent = "Start Speed Measurement";
playButton.onclick = () => engine.play();
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@
"lodash.memoize": "^4.1.2"
},
"devDependencies": {
"@babel/core": "^7.22.5",
"@babel/preset-env": "^7.22.5",
"@babel/core": "^7.22.17",
"@babel/preset-env": "^7.22.15",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-node-resolve": "^15.1.0",
"eslint": "^8.43.0",
"eslint-plugin-compat": "^4.1.4",
"eslint-plugin-import": "^2.27.5",
"@rollup/plugin-node-resolve": "^15.2.1",
"eslint": "^8.49.0",
"eslint-plugin-compat": "^4.2.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-json": "^3.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"prettier": "^2.8.8",
"lint-staged": "^14.0.1",
"prettier": "^3.0.3",
"rimraf": "^5.0.1",
"rollup": "^3.25.1",
"rollup-plugin-dts": "^5.3.0",
"typescript": "^5.1.3"
"rollup": "^3.29.1",
"rollup-plugin-dts": "^6.0.2",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=12"
Expand Down
4 changes: 2 additions & 2 deletions src/engines/BandwidthEngine/ParallelLatency.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BandwidthWithParallelLatencyEngine extends BandwidthEngine {
};

super.onRunningChange = this.#setLatencyRunning;
super.onConnectionError = () => this.#latencyEngine.stop();
super.onConnectionError = () => this.#latencyEngine.pause();
}
}

Expand Down Expand Up @@ -75,7 +75,7 @@ class BandwidthWithParallelLatencyEngine extends BandwidthEngine {

set onConnectionError(onConnectionError) {
super.onConnectionError = (...args) => {
this.#latencyEngine && this.#latencyEngine.stop();
this.#latencyEngine && this.#latencyEngine.pause();
onConnectionError(...args);
};
}
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ declare class SpeedTestEngine {
onRunningChange: (running: boolean) => void;
onResultsChange: ({ type: string }) => void;
onFinish: (results: Results) => void;
onError: (error: string) => void;
}

export default SpeedTestEngine;
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class MeasurementEngine {
this.#onFinish = f;
}

#onError = () => {}; // callback invoked if an error occurs during measurement
set onError(f) {
this.#onError = f;
}

// Public methods
pause() {
pausableTypes.includes(this.#curType()) && this.#curEngine.pause();
Expand Down Expand Up @@ -266,12 +271,16 @@ class MeasurementEngine {
engine.onConnectionError = e => {
msmResults.error = e;
this.onResultsChange({ type });
this.#onError(`Connection error while measuring packet loss: ${e}`);
this.#next();
};

engine.onCredentialsFailure = () => {
msmResults.error = 'unable to get turn server credentials';
this.onResultsChange({ type });
this.#onError(
'Error while measuring packet loss: unable to get turn server credentials.'
);
this.#next();
};

Expand Down Expand Up @@ -326,6 +335,7 @@ class MeasurementEngine {
engine.onConnectionError = e => {
msmResults.error = e;
this.onResultsChange({ type });
this.#onError(`Connection error while measuring latency: ${e}`);
this.#next();
};

Expand Down Expand Up @@ -438,6 +448,7 @@ class MeasurementEngine {
engine.onConnectionError = e => {
msmResults.error = e;
this.onResultsChange({ type });
this.#onError(`Connection error while measuring ${type}: ${e}`);
this.#next();
};

Expand Down
Loading

0 comments on commit 3b720e3

Please sign in to comment.