Skip to content

Commit

Permalink
Add user-level error handling for events
Browse files Browse the repository at this point in the history
If an event fails to decode pass the error and event data to the plugin
  • Loading branch information
emorydunn committed Mar 11, 2024
1 parent ac4e828 commit 7458c89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
16 changes: 4 additions & 12 deletions Sources/StreamDeck/StreamDeck Plugin/PluginCommunication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,14 @@ public final class PluginCommunication {
return
}

// Decode the event from the data
do {
// Decode the event from the data
let eventKey = try decoder.decode(ReceivableEvent.self, from: data)
try parseEvent(event: eventKey.event, context: eventKey.context, data: data)
} catch let error as DecodingError {
let json = String(decoding: data, as: UTF8.self)

log.error("""
Error decoding event:
\(error, privacy: .public)
\(json,privacy: .public)
""")

} catch {
let json = String(decoding: data, as: UTF8.self)
log.error("\(error.localizedDescription, privacy: .public)\n\(json, privacy: .public)")
// Pass the error onto the plugin
log.error("Failed to decode and parse the event received")
plugin.eventError(error, data: data)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
import OSLog

fileprivate let log = Logger(subsystem: "StreamDeckPlugin", category: "PluginDefaults")

public extension Plugin {

Expand All @@ -28,5 +31,21 @@ public extension Plugin {
func propertyInspectorDidAppear(action: String, context: String, device: String) { }

func propertyInspectorDidDisappear(action: String, context: String, device: String) { }


// MARK: Errors
func eventError<E: Error>(_ error: E, data: Data) {
if error is DecodingError {
let json = String(decoding: data, as: UTF8.self)

log.error("""
Error decoding event:
\(error, privacy: .public)
\(json, privacy: .public)
""")
} else {
let json = String(decoding: data, as: UTF8.self)
log.error("\(error.localizedDescription, privacy: .public)\n\(json, privacy: .public)")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,12 @@ public protocol Plugin {

/// Called immediately after `main()`.
static func pluginWasCreated()

// MARK: Errors
/// Called when the plugin encounters an error handling an event.
/// - Parameters:
/// - error: The error.
/// - data: The event data.
func eventError<E: Error>(_ error: E, data: Data)
}

0 comments on commit 7458c89

Please sign in to comment.