forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from ibi-group/bugsnag
Add ability to report errors to Bugsnag
- Loading branch information
Showing
7 changed files
with
107 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/main/java/org/opentripplanner/api/common/OTPExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/org/opentripplanner/standalone/ErrorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.opentripplanner.standalone; | ||
|
||
import com.bugsnag.Bugsnag; | ||
import com.bugsnag.Report; | ||
import com.bugsnag.Severity; | ||
import org.opentripplanner.common.MavenVersion; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A util class for reporting errors to the project defined by the Bugsnag project notifier API key. | ||
* | ||
* A Bugsnag project identifier key is unique to a Bugsnag project and allows errors to be saved against it. This key | ||
* can be obtained by logging into Bugsnag (https://app.bugsnag.com), clicking on Projects (left side menu) and | ||
* selecting the required project. Once selected, the notifier API key is presented. | ||
*/ | ||
public class ErrorUtils { | ||
private static Bugsnag bugsnag; | ||
private static final Logger LOG = LoggerFactory.getLogger(ErrorUtils.class); | ||
|
||
/** | ||
* Initialize Bugsnag using the project notifier API key when the application is first loaded. | ||
* @param params The parsed OTP command line parameters | ||
*/ | ||
public static void initializeBugsnagErrorReporting(CommandLineParameters params) { | ||
if (params.bugsnagKey != null) { | ||
bugsnag = new Bugsnag(params.bugsnagKey); | ||
bugsnag.setAppVersion(MavenVersion.VERSION.getShortVersionString()); | ||
if (params.bugsnagReleaseStage != null) { | ||
bugsnag.setReleaseStage(params.bugsnagReleaseStage); | ||
} | ||
if (params.bugsnagAppType != null) { | ||
bugsnag.setAppType(params.bugsnagAppType); | ||
} | ||
LOG.info("Bugsnag reporting enabled."); | ||
} else { | ||
LOG.warn("Bugsnag project notifier API key not available. Bugsnag error reporting disabled."); | ||
} | ||
} | ||
|
||
/** | ||
* If Bugsnag has been configured, report error based on provided information. | ||
*/ | ||
public static boolean reportErrorToBugsnag(String message, Throwable throwable) { | ||
return reportErrorToBugsnag(message, null, throwable); | ||
} | ||
|
||
/** | ||
* If Bugsnag has been configured, report error based on provided information. Note: throwable must be non-null. | ||
*/ | ||
public static boolean reportErrorToBugsnag(String message, Object badEntity, Throwable throwable) { | ||
// Log error to log output. | ||
LOG.error(message, throwable); | ||
|
||
// If bugsnag is disabled, make sure to report log full error. | ||
if (bugsnag == null) { | ||
LOG.warn("Bugsnag error reporting is disabled. Unable to report to Bugsnag this message: {} for this bad entity: {}", | ||
message, | ||
badEntity, | ||
throwable); | ||
return false; | ||
} | ||
|
||
// If no throwable provided, create a new UnknownError exception so that Bugsnag will accept the error report. | ||
if (throwable == null) { | ||
LOG.warn("No exception provided for this error report (message: {}). New UnknownError used instead.", message); | ||
throwable = new UnknownError("Exception type is unknown! Please add exception where report method is called."); | ||
} | ||
|
||
// Finally, construct report and send to bugsnag. | ||
Report report = bugsnag.buildReport(throwable); | ||
report.setContext(message); | ||
report.addToTab("debugging", "bad entity", badEntity != null ? badEntity.toString() : "N/A"); | ||
report.setSeverity(Severity.ERROR); | ||
return bugsnag.notify(report); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters