|
| 1 | +# Backtrace |
| 2 | +[](https://www.nuget.org/packages/Backtrace) |
| 3 | +<!-- [](https://ci.appveyor.com/project/konraddysput/backtrace-csharp) --> |
| 4 | + |
| 5 | +[](https://www.nuget.org/packages/Backtrace) |
| 6 | +<!-- [](https://ci.appveyor.com/project/konraddysput/backtrace-csharp/branch/dev) --> |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +[Backtrace](http://backtrace.io/)'s integration with Android applications written in Java allows customers to capture and report handled and unhandled java exceptions to their Backtrace instance, instantly offering the ability to prioritize and debug software errors. |
| 11 | + |
| 12 | + |
| 13 | +## Usage |
| 14 | +```java |
| 15 | +// replace with your endpoint url and token |
| 16 | +BacktraceCredentials credentials = new BacktraceCredentials("https://myserver.sp.backtrace.io:6097/", "4dca18e8769d0f5d10db0d1b665e64b3d716f76bf182fbcdad5d1d8070c12db0"); |
| 17 | +BacktraceClient backtraceClient = new BacktraceClient(getApplicationContext(), credentials); |
| 18 | + |
| 19 | +try { |
| 20 | + //throw exception here |
| 21 | +} catch (Exception exception) { |
| 22 | + backtraceClient.send(new BacktraceReport(e)); |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +# Table of contents |
| 27 | +1. [Features Summary](#features-summary) |
| 28 | +2. [Supported SKDs](#supported-sdks) |
| 29 | +3. [Differences and limitations of the SDKs version](#limitations) |
| 30 | +4. [Installation](#installation) |
| 31 | +5. [Running sample application](#sample-app) |
| 32 | +6. [Documentation](#documentation) |
| 33 | +7. [Architecture](#architecture) |
| 34 | + |
| 35 | + |
| 36 | +# Features Summary <a name="features-summary"></a> |
| 37 | +* Light-weight Java client library that quickly submits exceptions and crashes to your Backtrace dashboard. Can include callstack, system metadata, custom metadata.<!--, and file attachments if needed.--> |
| 38 | +* Supports a wide range of Android SDKs. |
| 39 | +* Supports asynchronous Tasks. |
| 40 | +* Fully customizable and extendable event handlers and base classes for custom implementations. |
| 41 | + |
| 42 | +# Differences and limitations of the SDKs version <a name="limitations"></a> |
| 43 | + |
| 44 | +# Supported SDKs <a name="supported-sdks"></a> |
| 45 | +* Minimal SDK version 19 (Android 4.4) |
| 46 | +* Target SDK version 28 (Android 9.0) |
| 47 | + |
| 48 | +# Installation <a name="installation"></a> |
| 49 | +TODO |
| 50 | + |
| 51 | +# Running sample application |
| 52 | +## Android Studio <a name="sample-app-android-studio"></a> |
| 53 | + |
| 54 | +- Open `MainActivity.java` class in **app\src\main\java\backtraceio\backtraceio** and replace `BacktraceCredential` constructor parameters with with your `Backtrace endpoint URL` (e.g. https://xxx.sp.backtrace.io:6098) and `submission token`: |
| 55 | + |
| 56 | +```java |
| 57 | +BacktraceCredentials credentials = new BacktraceCredentials("https://myserver.sp.backtrace.io:6097/", "4dca18e8769d0f5d10db0d1b665e64b3d716f76bf182fbcdad5d1d8070c12db0"); |
| 58 | +``` |
| 59 | + |
| 60 | +First start: |
| 61 | +- Press `Run` and `Run..` or type keys combination `Alt+Shift+F10`. |
| 62 | +- As module select `app` other options leave default. |
| 63 | +- Select `Run` and then select your emulator or connected device. |
| 64 | +- You should see new errors in your Backtrace instance. Refresh the Project page or Query Builder to see new details in real-time. |
| 65 | + |
| 66 | +# Documentation <a name="documentation"></a> |
| 67 | +## Initialize a new BacktraceClient <a name="documentation-initialization"></a> |
| 68 | + |
| 69 | +First create a `BacktraceCredential` instance with your `Backtrace endpoint URL` (e.g. https://xxx.sp.backtrace.io:6098) and `submission token`, and supply it as a parameter in the `BacktraceClient` constructor: |
| 70 | + |
| 71 | +```java |
| 72 | +BacktraceCredentials credentials = new BacktraceCredentials("https://myserver.sp.backtrace.io:6097/", "4dca18e8769d0f5d10db0d1b665e64b3d716f76bf182fbcdad5d1d8070c12db0"); |
| 73 | +BacktraceClient backtraceClient = new BacktraceClient(getApplicationContext(), credentials); |
| 74 | +``` |
| 75 | + |
| 76 | +## Sending an error report <a name="documentation-sending-report"></a> |
| 77 | + |
| 78 | +Methods `BacktraceClient.send` and `BacktraceClient.sendAsync` will send an error report to the Backtrace endpoint specified. There `send` method is overloaded, see examples below: |
| 79 | + |
| 80 | + |
| 81 | +### Using BacktraceReport |
| 82 | + |
| 83 | +The `BacktraceReport` class represents a single error report. (Optional) You can also submit custom attributes using the `attributes` parameter. <!--, or attach files by supplying an array of file paths in the `attachmentPaths` parameter.--> |
| 84 | + |
| 85 | +```java |
| 86 | +try { |
| 87 | + //throw exception here |
| 88 | +} catch (Exception exception) { |
| 89 | + BacktraceReport report = new BacktraceReport(e, |
| 90 | + new HashMap<String, Object>() {{ |
| 91 | + put("key", "value"); |
| 92 | + }}, new ArrayList<String>() {{ |
| 93 | + add("file_path_1"); |
| 94 | + add("file_path2"); |
| 95 | + }}); |
| 96 | + backtraceClient.send(report); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### Asynchronous Send Support |
| 101 | + |
| 102 | +Method `send` behind the mask use `AsyncTask` and wait until method `doInBackground` is not completed. Library gives you the option of not blocking code execution by using method `sendAsync` which returning the `AsyncTask<Void, Void, BacktraceResult>` object. Additionally, it is possible to specify the method that should be performed after completion `AsyncTask` by using events described in [events](#events). |
| 103 | + |
| 104 | + |
| 105 | +```java |
| 106 | +AsyncTask<Void, Void, BacktraceResult> sendAsyncTask = backtraceClient.sendAsync(report); |
| 107 | +// another code |
| 108 | +BacktraceResult result = sendAsyncTask.get(); |
| 109 | +``` |
| 110 | + |
| 111 | +### Other BacktraceReport Overloads |
| 112 | + |
| 113 | +`BacktraceClient` can also automatically create `BacktraceReport` given an exception or a custom message using the following overloads of the `BacktraceClient.send` or `BacktraceClient.sendAsync` methods: |
| 114 | + |
| 115 | +```java |
| 116 | +try { |
| 117 | + //throw exception here |
| 118 | +} catch (Exception exception) { |
| 119 | + |
| 120 | + backtraceClient.Send(report); |
| 121 | + |
| 122 | + //pass exception to Send method |
| 123 | + backtraceClient.Send(exception); |
| 124 | + |
| 125 | + //pass your custom message to Send method |
| 126 | + backtraceClient.SendAsync("Message"); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Attaching custom event handlers <a name="documentation-events"></a> |
| 131 | + |
| 132 | +All events are written in *listener* pattern. `BacktraceClient` allows you to attach your custom event handlers. For example, you can trigger actions before the `send` method: |
| 133 | + |
| 134 | +```java |
| 135 | +backtraceClient.setOnBeforeSendEventListener(new OnBeforeSendEventListener() { |
| 136 | + @Override |
| 137 | + public BacktraceData onEvent(BacktraceData data) { |
| 138 | + // another code |
| 139 | + return data; |
| 140 | + } |
| 141 | + }); |
| 142 | +``` |
| 143 | + |
| 144 | +`BacktraceClient` currently supports the following events: |
| 145 | +- `BeforeSend` |
| 146 | +- `AfterSend` |
| 147 | +- `RequestHandler` |
| 148 | +- `OnServerResponse` |
| 149 | +- `OnServerError` |
| 150 | + |
| 151 | + |
| 152 | +## Reporting unhandled application exceptions |
| 153 | +`BacktraceClient` also supports reporting of unhandled application exceptions not captured by your try-catch blocks. To enable reporting of unhandled exceptions: |
| 154 | +```java |
| 155 | +new BacktraceExceptionHandler(getApplicationContext(), backtraceClient); |
| 156 | +``` |
| 157 | + |
| 158 | +## Custom client and report classes <a name="documentation-customization"></a> |
| 159 | + |
| 160 | +You can extend `BacktraceBase` to create your own Backtrace client and error report implementation. You can refer to `BacktraceClient` for implementation inspirations. |
| 161 | + |
| 162 | +# Architecture <a name="architecture"></a> |
| 163 | + |
| 164 | +## BacktraceReport <a name="architecture-BacktraceReport"></a> |
| 165 | +**`BacktraceReport`** is a class that describe a single error report. |
| 166 | + |
| 167 | +## BacktraceClient <a name="architecture-BacktraceClient"></a> |
| 168 | +**`BacktraceClient`** is a class that allows you to instantiate a client instance that interacts with `BacktraceApi`. This class sets up connection to the Backtrace endpoint and manages error reporting behavior. `BacktraceClient` extends `BacktraceBase` class. |
| 169 | + |
| 170 | +## BacktraceData <a name="architecture-BacktraceData"></a> |
| 171 | +**`BacktraceData`** is a serializable class that holds the data to create a diagnostic JSON to be sent to the Backtrace endpoint via `BacktraceApi`. You can add additional pre-processors for `BacktraceData` by attaching an event handler to the `BacktraceClient.setOnBeforeSendEventListener(event)` event. `BacktraceData` require `BacktraceReport` and `BacktraceClient` client attributes. |
| 172 | + |
| 173 | +## BacktraceApi <a name="architecture-BacktraceApi"></a> |
| 174 | +**`BacktraceApi`** is a class that sends diagnostic JSON to the Backtrace endpoint. `BacktraceApi` is instantiated when the `BacktraceClient` constructor is called. You use the following event handlers in `BacktraceApi` to customize how you want to handle JSON data: |
| 175 | +- `RequestHandler` - attach an event handler to this event to override the default `BacktraceApi.send` method. <!--A `RequestHandler` TODO --> |
| 176 | +- `OnServerError` - attach an event handler to be invoked when the server returns with a `400 bad request`, `401 unauthorized` or other HTTP error codes. <!-- TODO --> |
| 177 | +- `OnServerResponse` - attach an event handler to be invoked when the server returns with a valid response. |
| 178 | + |
| 179 | + |
| 180 | +## BacktraceResult <a name="architecture-BacktraceResult"></a> |
| 181 | +**`BacktraceResult`** is a class that holds response and result from a `send` or `sendAsync` call. The class contains a `Status` property that indicates whether the call was completed (`OK`), the call returned with an error (`ServerError`), . Additionally, the class has a `Message` property that contains details about the status. |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
0 commit comments