PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/bugsnag-android-core/src/main/java/com/bugsnag/android/Event.java

https://github.com/bugsnag/bugsnag-android
Java | 415 lines | 234 code | 49 blank | 132 comment | 44 complexity | ec9cda5ba86d5ea09399a6e4e23f8aae MD5 | raw file
  1. package com.bugsnag.android;
  2. import com.bugsnag.android.internal.ImmutableConfig;
  3. import androidx.annotation.NonNull;
  4. import androidx.annotation.Nullable;
  5. import java.io.IOException;
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10. /**
  11. * An Event object represents a Throwable captured by Bugsnag and is available as a parameter on
  12. * an {@link OnErrorCallback}, where individual properties can be mutated before an error report is
  13. * sent to Bugsnag's API.
  14. */
  15. @SuppressWarnings("ConstantConditions")
  16. public class Event implements JsonStream.Streamable, MetadataAware, UserAware, FeatureFlagAware {
  17. private final EventInternal impl;
  18. private final Logger logger;
  19. Event(@Nullable Throwable originalError,
  20. @NonNull ImmutableConfig config,
  21. @NonNull SeverityReason severityReason,
  22. @NonNull Logger logger) {
  23. this(originalError, config, severityReason, new Metadata(), new FeatureFlags(), logger);
  24. }
  25. Event(@Nullable Throwable originalError,
  26. @NonNull ImmutableConfig config,
  27. @NonNull SeverityReason severityReason,
  28. @NonNull Metadata metadata,
  29. @NonNull FeatureFlags featureFlags,
  30. @NonNull Logger logger) {
  31. this(new EventInternal(originalError, config, severityReason, metadata, featureFlags),
  32. logger);
  33. }
  34. Event(@NonNull EventInternal impl, @NonNull Logger logger) {
  35. this.impl = impl;
  36. this.logger = logger;
  37. }
  38. private void logNull(String property) {
  39. logger.e("Invalid null value supplied to config." + property + ", ignoring");
  40. }
  41. /**
  42. * The Throwable object that caused the event in your application.
  43. *
  44. * Manipulating this field does not affect the error information reported to the
  45. * Bugsnag dashboard. Use {@link Event#getErrors()} to access and amend the representation of
  46. * the error that will be sent.
  47. */
  48. @Nullable
  49. public Throwable getOriginalError() {
  50. return impl.getOriginalError();
  51. }
  52. /**
  53. * Information extracted from the {@link Throwable} that caused the event can be found in this
  54. * field. The list contains at least one {@link Error} that represents the thrown object
  55. * with subsequent elements in the list populated from {@link Throwable#getCause()}.
  56. *
  57. * A reference to the actual {@link Throwable} object that caused the event is available
  58. * through {@link Event#getOriginalError()} ()}.
  59. */
  60. @NonNull
  61. public List<Error> getErrors() {
  62. return impl.getErrors();
  63. }
  64. /**
  65. * If thread state is being captured along with the event, this field will contain a
  66. * list of {@link Thread} objects.
  67. */
  68. @NonNull
  69. public List<Thread> getThreads() {
  70. return impl.getThreads();
  71. }
  72. /**
  73. * A list of breadcrumbs leading up to the event. These values can be accessed and amended
  74. * if necessary. See {@link Breadcrumb} for details of the data available.
  75. */
  76. @NonNull
  77. public List<Breadcrumb> getBreadcrumbs() {
  78. return impl.getBreadcrumbs();
  79. }
  80. /**
  81. * Information set by the notifier about your app can be found in this field. These values
  82. * can be accessed and amended if necessary.
  83. */
  84. @NonNull
  85. public AppWithState getApp() {
  86. return impl.getApp();
  87. }
  88. /**
  89. * Information set by the notifier about your device can be found in this field. These values
  90. * can be accessed and amended if necessary.
  91. */
  92. @NonNull
  93. public DeviceWithState getDevice() {
  94. return impl.getDevice();
  95. }
  96. /**
  97. * The API key used for events sent to Bugsnag. Even though the API key is set when Bugsnag
  98. * is initialized, you may choose to send certain events to a different Bugsnag project.
  99. */
  100. public void setApiKey(@NonNull String apiKey) {
  101. if (apiKey != null) {
  102. impl.setApiKey(apiKey);
  103. } else {
  104. logNull("apiKey");
  105. }
  106. }
  107. /**
  108. * The API key used for events sent to Bugsnag. Even though the API key is set when Bugsnag
  109. * is initialized, you may choose to send certain events to a different Bugsnag project.
  110. */
  111. @NonNull
  112. public String getApiKey() {
  113. return impl.getApiKey();
  114. }
  115. /**
  116. * The severity of the event. By default, unhandled exceptions will be {@link Severity#ERROR}
  117. * and handled exceptions sent with {@link Bugsnag#notify} {@link Severity#WARNING}.
  118. */
  119. public void setSeverity(@NonNull Severity severity) {
  120. if (severity != null) {
  121. impl.setSeverity(severity);
  122. } else {
  123. logNull("severity");
  124. }
  125. }
  126. /**
  127. * The severity of the event. By default, unhandled exceptions will be {@link Severity#ERROR}
  128. * and handled exceptions sent with {@link Bugsnag#notify} {@link Severity#WARNING}.
  129. */
  130. @NonNull
  131. public Severity getSeverity() {
  132. return impl.getSeverity();
  133. }
  134. /**
  135. * Set the grouping hash of the event to override the default grouping on the dashboard.
  136. * All events with the same grouping hash will be grouped together into one error. This is an
  137. * advanced usage of the library and mis-using it will cause your events not to group properly
  138. * in your dashboard.
  139. *
  140. * As the name implies, this option accepts a hash of sorts.
  141. */
  142. public void setGroupingHash(@Nullable String groupingHash) {
  143. impl.setGroupingHash(groupingHash);
  144. }
  145. /**
  146. * Set the grouping hash of the event to override the default grouping on the dashboard.
  147. * All events with the same grouping hash will be grouped together into one error. This is an
  148. * advanced usage of the library and mis-using it will cause your events not to group properly
  149. * in your dashboard.
  150. *
  151. * As the name implies, this option accepts a hash of sorts.
  152. */
  153. @Nullable
  154. public String getGroupingHash() {
  155. return impl.getGroupingHash();
  156. }
  157. /**
  158. * Sets the context of the error. The context is a summary what what was occurring in the
  159. * application at the time of the crash, if available, such as the visible activity.
  160. */
  161. public void setContext(@Nullable String context) {
  162. impl.setContext(context);
  163. }
  164. /**
  165. * Returns the context of the error. The context is a summary what what was occurring in the
  166. * application at the time of the crash, if available, such as the visible activity.
  167. */
  168. @Nullable
  169. public String getContext() {
  170. return impl.getContext();
  171. }
  172. /**
  173. * Sets the user associated with the event.
  174. */
  175. @Override
  176. public void setUser(@Nullable String id, @Nullable String email, @Nullable String name) {
  177. impl.setUser(id, email, name);
  178. }
  179. /**
  180. * Returns the currently set User information.
  181. */
  182. @Override
  183. @NonNull
  184. public User getUser() {
  185. return impl.getUser();
  186. }
  187. /**
  188. * Adds a map of multiple metadata key-value pairs to the specified section.
  189. */
  190. @Override
  191. public void addMetadata(@NonNull String section, @NonNull Map<String, ?> value) {
  192. if (section != null && value != null) {
  193. impl.addMetadata(section, value);
  194. } else {
  195. logNull("addMetadata");
  196. }
  197. }
  198. /**
  199. * Adds the specified key and value in the specified section. The value can be of
  200. * any primitive type or a collection such as a map, set or array.
  201. */
  202. @Override
  203. public void addMetadata(@NonNull String section, @NonNull String key, @Nullable Object value) {
  204. if (section != null && key != null) {
  205. impl.addMetadata(section, key, value);
  206. } else {
  207. logNull("addMetadata");
  208. }
  209. }
  210. /**
  211. * Removes all the data from the specified section.
  212. */
  213. @Override
  214. public void clearMetadata(@NonNull String section) {
  215. if (section != null) {
  216. impl.clearMetadata(section);
  217. } else {
  218. logNull("clearMetadata");
  219. }
  220. }
  221. /**
  222. * Removes data with the specified key from the specified section.
  223. */
  224. @Override
  225. public void clearMetadata(@NonNull String section, @NonNull String key) {
  226. if (section != null && key != null) {
  227. impl.clearMetadata(section, key);
  228. } else {
  229. logNull("clearMetadata");
  230. }
  231. }
  232. /**
  233. * Returns a map of data in the specified section.
  234. */
  235. @Override
  236. @Nullable
  237. public Map<String, Object> getMetadata(@NonNull String section) {
  238. if (section != null) {
  239. return impl.getMetadata(section);
  240. } else {
  241. logNull("getMetadata");
  242. return null;
  243. }
  244. }
  245. /**
  246. * Returns the value of the specified key in the specified section.
  247. */
  248. @Override
  249. @Nullable
  250. public Object getMetadata(@NonNull String section, @NonNull String key) {
  251. if (section != null && key != null) {
  252. return impl.getMetadata(section, key);
  253. } else {
  254. logNull("getMetadata");
  255. return null;
  256. }
  257. }
  258. /**
  259. * {@inheritDoc}
  260. */
  261. @Override
  262. public void addFeatureFlag(@NonNull String name) {
  263. if (name != null) {
  264. impl.addFeatureFlag(name);
  265. } else {
  266. logNull("addFeatureFlag");
  267. }
  268. }
  269. /**
  270. * {@inheritDoc}
  271. */
  272. @Override
  273. public void addFeatureFlag(@NonNull String name, @Nullable String variant) {
  274. if (name != null) {
  275. impl.addFeatureFlag(name, variant);
  276. } else {
  277. logNull("addFeatureFlag");
  278. }
  279. }
  280. /**
  281. * {@inheritDoc}
  282. */
  283. @Override
  284. public void addFeatureFlags(@NonNull Iterable<FeatureFlag> featureFlags) {
  285. if (featureFlags != null) {
  286. impl.addFeatureFlags(featureFlags);
  287. } else {
  288. logNull("addFeatureFlags");
  289. }
  290. }
  291. /**
  292. * {@inheritDoc}
  293. */
  294. @Override
  295. public void clearFeatureFlag(@NonNull String name) {
  296. if (name != null) {
  297. impl.clearFeatureFlag(name);
  298. } else {
  299. logNull("clearFeatureFlag");
  300. }
  301. }
  302. /**
  303. * {@inheritDoc}
  304. */
  305. @Override
  306. public void clearFeatureFlags() {
  307. impl.clearFeatureFlags();
  308. }
  309. @Override
  310. public void toStream(@NonNull JsonStream stream) throws IOException {
  311. impl.toStream(stream);
  312. }
  313. /**
  314. * Whether the event was a crash (i.e. unhandled) or handled error in which the system
  315. * continued running.
  316. *
  317. * Unhandled errors count towards your stability score. If you don't want certain errors
  318. * to count towards your stability score, you can alter this property through an
  319. * {@link OnErrorCallback}.
  320. */
  321. public boolean isUnhandled() {
  322. return impl.getUnhandled();
  323. }
  324. /**
  325. * Whether the event was a crash (i.e. unhandled) or handled error in which the system
  326. * continued running.
  327. *
  328. * Unhandled errors count towards your stability score. If you don't want certain errors
  329. * to count towards your stability score, you can alter this property through an
  330. * {@link OnErrorCallback}.
  331. */
  332. public void setUnhandled(boolean unhandled) {
  333. impl.setUnhandled(unhandled);
  334. }
  335. protected boolean shouldDiscardClass() {
  336. return impl.shouldDiscardClass();
  337. }
  338. protected void updateSeverityInternal(@NonNull Severity severity) {
  339. impl.updateSeverityInternal(severity);
  340. }
  341. protected void updateSeverityReason(@NonNull @SeverityReason.SeverityReasonType String reason) {
  342. impl.updateSeverityReason(reason);
  343. }
  344. void setApp(@NonNull AppWithState app) {
  345. impl.setApp(app);
  346. }
  347. void setDevice(@NonNull DeviceWithState device) {
  348. impl.setDevice(device);
  349. }
  350. void setBreadcrumbs(@NonNull List<Breadcrumb> breadcrumbs) {
  351. impl.setBreadcrumbs(breadcrumbs);
  352. }
  353. @Nullable
  354. Session getSession() {
  355. return impl.session;
  356. }
  357. void setSession(@Nullable Session session) {
  358. impl.session = session;
  359. }
  360. EventInternal getImpl() {
  361. return impl;
  362. }
  363. void setRedactedKeys(Collection<String> redactedKeys) {
  364. impl.setRedactedKeys(redactedKeys);
  365. }
  366. }