APIs for Cordova
Learn more about APIs of the SDK.
This page shows all available top-level APIs of the SDK. You can use these APIs as the primary way to:
- Configure the SDK after initialization
- Manually capture different types of events
- Enrich events with additional data
- ... and more!
These APIs are functions that you can use as follows - they are all available on the top-level Sentry object:
import * as Sentry from "sentry-cordova";
Sentry.setTag("tag", "value");
import * as Sentry from "sentry-cordova";
Sentry.setTag("tag", "value");
init
function init(options: InitOptions): Client | undefined
Initialize the SDK with the given options. See Options for the options you can pass to init.
addEventProcessor
function addEventProcessor(processor: EventProcessor): void
Adds an event processor to the SDK. An event processor receives every event before it is sent to Sentry. It can either mutate the event (and return it) or return null to discard the event. Event processors can also return a promise, but it is recommended to use this only when necessary as it slows down event processing.
Event processors added via Sentry.addEventProcessor() will be applied to all events in your application. If you want to add an event processor that only applies to certain events, you can also add one to a scope as follows:
Sentry.withScope((scope) => {
scope.addEventProcessor((event) => {
// this will only be applied to events captured within this scope
return event;
});
Sentry.captureException(new Error("test"));
});
Sentry.withScope((scope) => {
scope.addEventProcessor((event) => {
// this will only be applied to events captured within this scope
return event;
});
Sentry.captureException(new Error("test"));
});
nativeCrash
function nativeCrash(): void
Crashes the app on the native layer, if the native SDK is enabled. Only use this to test that native crashes are reported to Sentry.
captureException
function captureException( exception: unknown, captureContext?: CaptureContext ): EventId
Capture an exception event and send it to Sentry. Note that you can pass not only Error objects, but also other objects as exception - in that case, the SDK will attempt to serialize the object for you, and the stack trace will be generated by the SDK and may be less accurate.
captureMessage
function captureMessage( message: string, captureContext?: CaptureContext | SeverityLevel ): EventId
Capture a message event and send it to Sentry. Optionally, instead of a CaptureContext, you can also pass a SeverityLevel as second argument, e.g. "error" or "warning".
Messages show up as issues on your issue stream, with the message as the issue name.
setTag
function setTag(key: string, value: string): void
Set a tag to be sent with Sentry events.
- Tag keys have a maximum length of 200 characters and can contain only letters (
a-zA-Z), numbers (0-9), underscores (_), periods (.), colons (:), and dashes (-). - Tag values have a maximum length of 200 characters and they cannot contain the newline (
\n) character.
setTags
function setTags(tags: Record<string, string>): void
Set multiple tags to be sent with Sentry events.
setContext
function setContext(name: string, context: Record<string, unknown>): void
Set a context to be sent with Sentry events. Custom contexts allow you to attach arbitrary data to an event. You cannot search these, but they are viewable on the issue page - if you need to be able to filter for certain data, use tags instead. Pass null as the context value to clear a previously set context.
There are no restrictions on context name. In the context object, all keys are allowed except for type, which is used internally.
By default, Sentry SDKs normalize nested structured context data up to three levels deep. Any data beyond this depth will be trimmed and marked using its type instead. To adjust this default, use the normalizeDepth SDK option.
Learn more about conventions for common contexts in the contexts interface developer documentation.
setExtra
function setExtra(name: string, extra: unknown): void
Set additional data to be sent with Sentry events.
setExtras
function setExtras(extras: Record<string, unknown>): void
Set multiple additional data entries to be sent with Sentry events.
setUser
function setUser(user: User | null): void
Set a user to be sent with Sentry events. Set to null to unset the user. In addition to the specified properties of the User object, you can also add additional arbitrary key/value pairs.
addBreadcrumb
function addBreadcrumb(breadcrumb: Breadcrumb, hint?: Hint): void
You can manually add breadcrumbs whenever something interesting happens. For example, you might manually record a breadcrumb if the user authenticates or another state change occurs.
startSpan
function startSpan<T>(options: StartSpanOptions, callback: (span: Span) => T): T
Starts a new span, that is active in the provided callback. This span will be a child of the currently active span, if there is one.
Any spans created inside of the callback will be children of this span.
The started span will automatically be ended when the callback returns, and will thus measure the duration of the callback. The callback can also be an async function.
See Tracing Instrumentation for more information on how to work with spans.
startInactiveSpan
function startInactiveSpan<T>(options: StartSpanOptions): Span
Starts a new span. This span will be a child of the currently active span, if there is one. The returned span has to be ended manually via span.end() when the span is done.
See Tracing Instrumentation for more information on how to work with spans.
startSpanManual
function startSpanManual<T>(options: StartSpanOptions, callback: (span: Span) => T): T
Starts a new span, that is active in the provided callback. This span will be a child of the currently active span, if there is one.
Any spans created inside of the callback will be children of this span.
The started span will not automatically end - you have to call span.end() when the span is done. Please note that the span will still only be the parent span of spans created inside of the callback, while the callback is active. In most cases, you will want to use startSpan or startInactiveSpan instead.
See Tracing Instrumentation for more information on how to work with spans.
See Scopes for more information on how to use scopes.
withScope
function withScope(callback: (scope: Scope) => void): void
Forks the current scope and calls the callback with the forked scope.
getCurrentScope
function getCurrentScope(): Scope
Returns the current scope.
Note that in most cases you should not use this API, but instead use withScope to generate and access a local scope. There are no guarantees about the consistency of getCurrentScope across different parts of your application, as scope forking may happen under the hood at various points.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").