[Cocoa] Report errors from "do - catch"

Hi,

I’m implementing Sentry on my iOS app, and I would like to be able to send handled errors that occur on do { ... } catch { ... } blocks.

Is there any way with the SDK to send an Error to Sentry?

1 Like

Short answer: no.

Long answer:
Depending if you are using Swift or Objc Error can be something different right?
So is it Error or NSError?
Also an error on Cocoa is not really helpful since it usually does not contain a stack trace or any other useful information besides the title and the message.
So I would go and call:
https://docs.sentry.io/clients/cocoa/advanced/#sending-events
or if you want to have a stacktrace:
https://docs.sentry.io/clients/cocoa/advanced/#adding-stacktrace-to-message

Hope this helps.

Thanks for the quick reply! Agree that an error on Cocoa isn’t really helpful, but it’s always good to have a reference to it, specially if it’s a custom error where you added some helpful information about it.

Anyway, I created a wrapper to log an error like this:

static func log(error: Error, message: String?) {
    Sentry.Client.shared?.snapshotStacktrace {
        let error = error as NSError

        let exception = Sentry.Exception(
            value: "\(error.domain).\(error.code)",
            type: error.domain
        )
        exception.thread?.crashed = 0

        let event = Sentry.Event(level: .error)
        event.logger = "****.Logger"
        event.exceptions = [exception]
        event.message = message ?? ""
        event.extra = [
            "error_description": error.description,
            "error_localized_description": error.localizedDescription,
        ]

        Sentry.Client.shared?.appendStacktrace(to: event)
        Sentry.Client.shared?.send(event: event, completion: nil)
    }
}
1 Like