- Go to your bugsnag dashboard and press 'NEW PROJECT'
- For the question 'Where does your application run?' Choose 'Server'
- For the question 'What platform or programming language are you using?' Choose 'Javascript'
- For the question 'What framework are you using?' Choose 'Other'
- Name the project and press 'continue'
- Make sure you add the following to your env file
When running local, to prevent Bugsnag from being filled with errors, set BUGSNAG_ACTIVE to false OR do not declare it. (If not declared messages will also not be sent to Bugsnag.
BUGSNAG_API_KEY=<YOUR BUGSNAG API KEY>
BUGSNAG_STANDARD_SEVERITY=<STANDARD SEVERITY LEVEL FOR BUGSNAG (info OR warning OR error)>
BUGSNAG_ACTIVE=<true OR false, depending on whether bugsnag should be ACTIVE>
- Test if the module is working by sending an exception to bugsnag using the following code or CLI command
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag->sendException(new RuntimeException('Test exception'));
For sending a basic error to Bugsnag, use the following code
// Here it is important that the require is pointing to the correct path. Point it to the path where you've installed the composer package.
const Bugsnag = require('/vendor/Violet88/BugsnagVueModule/src/js/BugsnagVue.js');
Bugsnag.start();
try{
something.risky();
}catch(e){
Bugsnag.notify(e);
}
For this CLI command to work, make sure you configure the routes correctly. This can be done by adding the following to your routes.yml
SilverStripe\Control\Director:
rules:
'bugsnag//build': 'Violet88\BugsnagModule\BugsnagController'
'bugsnag//initial': 'Violet88\BugsnagModule\BugsnagController'
Then, run the following command in your terminal
./vendor/bin/sake bugsnag/initial
php vendor/silverstripe/framework/cli-script.php bugsnag/initial
- If everything is setup correctly, you'll see the exception in your bugsnag dashboard
- For the Vue part of the module to work you have to run:
npm install dotenv webpack webpack-bugsnag-plugins
Then add the following to your composer.json
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
],
and add the following to your webpack.mix.js
require('dotenv').config();
let webpack = require('webpack');
const { BugsnagSourceMapUploaderPlugin } = require('webpack-bugsnag-plugins');
const PACKAGE_VERSION = process.env.npm_package_version
let dotenvplugin = new webpack.DefinePlugin({
'process.env': {
'BUGSNAG_API_KEY': JSON.stringify(process.env.BUGSNAG_API_KEY),
'BUGSNAG_ACTIVE': JSON.stringify(process.env.BUGSNAG_ACTIVE),
'VERSION': JSON.stringify(PACKAGE_VERSION)
}
});
mix.options({legacyNodePolyfills: false})
mix.webpackConfig({
output: {
library: 'BugsnagVue',
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: 'this'
},
plugins: [
dotenvplugin,
new BugsnagSourceMapUploaderPlugin({
apiKey: process.env.BUGSNAG_API_KEY,
appVersion: PACKAGE_VERSION ?? '1.0.0',
overwrite: true,
publicPath: '*'
})
]
});
Also make sure you generate the sourcemaps, for example by adding .sourceMaps(true, 'source-map')
between mix.js()). This could look as follows:
mix.sourceMaps(true, 'source-map').js([
`${theme}/javascript/bundle.js`,
//'vendor/violet88github/silverstripe-bugsnag-module-vue/src/js/BugsnagVue.js'
], `${theme}/dist/js/bundle.js`);
use Violet88\BugsnagModule\Bugsnag;
use Exception;
use SilverStripe\Core\Injector\Injector;
try{
//do something
} catch (Exception $e) {
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag->sendException($e);
}
To catch errors in Vue, add the following as high up in your javascript stream as possible:
const Bugsnag = require('/vendor/Violet88/BugsnagVueModule/src/js/BugsnagVue.js');
Bugsnag.start();
Add to Vue:
Vue.createApp(App)
.use(Bugsnag.getVuePlugin())
.mount('#app')
use Violet88\BugsnagModule\Bugsnag;
use SilverStripe\Core\Injector\Injector;
$bugsnag = Injector::inst()->get(Bugsnag::class);
// Arguments are in the following order: repository, revision, provider, builderName
$bugsnag->notifyBuild('https://github.com/Violet88github/bugsnag-module', '1.0.0', 'github', 'Sven');
- Add the following to your routes yaml
SilverStripe\Control\Director:
rules:
'bugsnag//build': 'Violet88\BugsnagModule\BugsnagController'
'bugsnag//initial': 'Violet88\BugsnagModule\BugsnagController'
- After that run one of the following commands
run the following command in your project root, replacing these arguments with your own.
vendor/bin/sake bugsnag/build "repository=https://github.com/Violet88github/bugsnag-module&revision=1.0.0&provider=github&builderName=Sven"
run the following command in your project root, replacing these arguments with your own.
php vendor/silverstripe/framework/cli-script.php bugsnag/build repository=https://github.com/Violet88github/bugsnag-module revision=1.0.0 provider=github builderName=sven
- Add the following to your configuration yaml
SilverStripe\Core\Injector\Injector:
Psr\Log\LoggerInterface:
calls:
BugsnagHandler: [pushHandler, ['%$BugsnagHandler']]
BugsnagHandler:
class: Violet88\BugsnagModule\BugsnagLogger
constructor:
- '%$Violet88\BugsnagModule\Bugsnag'
- Now any unhandled errors will be sent to Bugsnag
Using switches or custom metadata, you can configure the Bugsnag error message. The following standard switches are available and are chainable.
If a user is logged in, you can use the addUserInfo() switch to add their user info to the Bugsnag message. In the code this would work as follows:
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->addUserInfo() //Add false as an argument to remove user info
->sendException(new RuntimeException('Test exception'));
If you have set a project version in your composer.json, you can use the addVersion() switch to add or remove the version in the Bugsnag message. In the code this would work as follows:
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->addVersion() //Add false as an argument toremove the version
->sendException(new RuntimeException('Test exception'));
To add a simple list of packages that are installed in the project, you can use the addInstalledPackages() switch. This list does not include versions, if you wish to include those continue to the next switch. In the code this would work as follows:
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->addPackages() //Add false as an argument to remove the list of packages
->sendException(new RuntimeException('Test exception'));
This will automatically retrieve the version as set in your composer.json
To add a list of packages that are installed in the project, including their versions, you can use the addPackagesWithVersions() switch. In the code this would work as follows:
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->addPackagesWithVersions() //Add false as an argument to remove the list of packages
->sendException(new RuntimeException('Test exception'));
The following custom metadata functions are available
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->setAppVersion('1.0.0') /* If this version corresponds to an existing release version in Bugsnag, it will be findable under that release in the dashboard */
->sendException(new RuntimeException('Test exception'));
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->setReleaseStage('production')
->sendException(new RuntimeException('Test exception'));
This can be useful when you are using an on premise Bugsnag server
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->setEndpoint('https://bugsnag.example.com')
->sendException(new RuntimeException('Test exception'));
If all else falls short, you can always add your own key => value pair to the metadata.
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->addExtraOption('key', 'value')
->sendException(new RuntimeException('Test exception'));
To remove certain metadata, you can use the removeExtraOption() function
$bugsnag = Injector::inst()->get(Bugsnag::class);
$bugsnag
->removeExtraOption('key')
->sendException(new RuntimeException('Test exception'));
To customize the Bugsnag error in javascript, have a look at the Bugsnag documentation. The same customization options should be available in this module.