Skip to content

Commit

Permalink
Merge pull request #382 from wbobeirne/fix-opener-error
Browse files Browse the repository at this point in the history
Catch uncaught opener errors
  • Loading branch information
th0r committed Sep 28, 2020
2 parents e4b2677 + b0f717b commit e4a8974
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,8 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
## UNRELEASED

<!-- Add changelog entries for new changes under this section -->
* **Bug Fix**
* Prevent crashes when `openAnalyzer` was set to true in environments where there's no program to handle opening. ([#382](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/382) by [@wbobeirne](https://github.com/wbobeirne))

## 3.9.0

Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
@@ -1,5 +1,6 @@
const {inspect} = require('util');
const _ = require('lodash');
const opener = require('opener');

const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

Expand Down Expand Up @@ -51,3 +52,14 @@ exports.defaultTitle = function () {

return `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${currentTime}]`;
};

/**
* Calls opener on a URI, but silently try / catches it.
*/
exports.open = function (uri, logger) {
try {
opener(uri);
} catch (err) {
logger.debug(`Opener failed to open "${uri}":\n${err}`);
}
};
6 changes: 3 additions & 3 deletions src/viewer.js
Expand Up @@ -6,12 +6,12 @@ const WebSocket = require('ws');
const _ = require('lodash');
const express = require('express');
const ejs = require('ejs');
const opener = require('opener');
const mkdir = require('mkdirp');
const {bold} = require('chalk');

const Logger = require('./Logger');
const analyzer = require('./analyzer');
const {open} = require('./utils');

const projectRoot = path.resolve(__dirname, '..');
const assetsRoot = path.join(projectRoot, 'public');
Expand Down Expand Up @@ -85,7 +85,7 @@ async function startServer(bundleStats, opts) {
);

if (openBrowser) {
opener(url);
open(url, logger);
}
});
});
Expand Down Expand Up @@ -169,7 +169,7 @@ async function generateReport(bundleStats, opts) {
logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);

if (openBrowser) {
opener(`file://${reportFilepath}`);
open(`file://${reportFilepath}`, logger);
}
resolve();
} catch (e) {
Expand Down

0 comments on commit e4a8974

Please sign in to comment.