enhance errors

pull/340/head
Lucas Nogueira 2 years ago
parent 5542fbae4e
commit c80c3182e5
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7

@ -5,19 +5,36 @@ enum NotificationError: LocalizedError {
case contentNoId case contentNoId
case contentNoTitle case contentNoTitle
case contentNoBody case contentNoBody
case triggerConstructionFailed
case triggerRepeatIntervalTooShort case triggerRepeatIntervalTooShort
case attachmentNoId case attachmentNoId
case attachmentNoUrl case attachmentNoUrl
case attachmentFileNotFound(path: String) case attachmentFileNotFound(path: String)
case attachmentUnableToCreate(String) case attachmentUnableToCreate(String)
case pastScheduledTime
case invalidDate(String)
var errorDescription: String? { var errorDescription: String? {
switch self { switch self {
case .contentNoId:
return "Missing notification identifier"
case .contentNoTitle:
return "Missing notification title"
case .contentNoBody:
return "Missing notification body"
case .triggerRepeatIntervalTooShort:
return "Schedule interval too short, must be a least 1 minute"
case .attachmentNoId:
return "Missing attachment identifier"
case .attachmentNoUrl:
return "Missing attachment URL"
case .attachmentFileNotFound(let path): case .attachmentFileNotFound(let path):
return "Unable to find file \(path) for attachment" return "Unable to find file \(path) for attachment"
default: case .attachmentUnableToCreate(let error):
return "" return "Failed to create attachment: \(error)"
case .pastScheduledTime:
return "Scheduled time must be *after* current time"
case .invalidDate(let date):
return "Could not parse date \(date)"
} }
} }
} }
@ -127,7 +144,7 @@ func makeAttachmentOptions(_ options: JSObject) -> JSObject {
return opts return opts
} }
func handleScheduledNotification(_ invoke: Invoke, _ schedule: JSObject) throws func handleScheduledNotification(_ schedule: JSObject) throws
-> UNNotificationTrigger? -> UNNotificationTrigger?
{ {
let kind = schedule["kind"] as? String ?? "" let kind = schedule["kind"] as? String ?? ""
@ -145,8 +162,7 @@ func handleScheduledNotification(_ invoke: Invoke, _ schedule: JSObject) throws
let dateInfo = Calendar.current.dateComponents(in: TimeZone.current, from: at) let dateInfo = Calendar.current.dateComponents(in: TimeZone.current, from: at)
if dateInfo.date! < Date() { if dateInfo.date! < Date() {
invoke.reject("Scheduled time must be *after* current time") throw NotificationError.pastScheduledTime
return nil
} }
let dateInterval = DateInterval(start: Date(), end: dateInfo.date!) let dateInterval = DateInterval(start: Date(), end: dateInfo.date!)
@ -160,7 +176,7 @@ func handleScheduledNotification(_ invoke: Invoke, _ schedule: JSObject) throws
timeInterval: dateInterval.duration, repeats: repeats) timeInterval: dateInterval.duration, repeats: repeats)
} else { } else {
invoke.reject("could not parse `at` date \(date)") throw NotificationError.invalidDate(date)
} }
case "Interval": case "Interval":
let dateComponents = getDateComponents(payload) let dateComponents = getDateComponents(payload)

@ -1,7 +1,21 @@
import Tauri import Tauri
import UserNotifications import UserNotifications
public func makeCategories(_ actionTypes: [JSObject]) { enum CategoryError: LocalizedError {
case noId
case noActionId
var errorDescription: String? {
switch self {
case .noId:
return "Action type `id` missing"
case .noActionId:
return "Action `id` missing"
}
}
}
public func makeCategories(_ actionTypes: [JSObject]) throws {
var createdCategories = [UNNotificationCategory]() var createdCategories = [UNNotificationCategory]()
let generalCategory = UNNotificationCategory( let generalCategory = UNNotificationCategory(
@ -13,13 +27,12 @@ public func makeCategories(_ actionTypes: [JSObject]) {
createdCategories.append(generalCategory) createdCategories.append(generalCategory)
for type in actionTypes { for type in actionTypes {
guard let id = type["id"] as? String else { guard let id = type["id"] as? String else {
Logger.error("Action type must have an id field") throw CategoryError.noId
continue
} }
let hiddenBodyPlaceholder = type["hiddenPreviewsBodyPlaceholder"] as? String ?? "" let hiddenBodyPlaceholder = type["hiddenPreviewsBodyPlaceholder"] as? String ?? ""
let actions = type["actions"] as? [JSObject] ?? [] let actions = type["actions"] as? [JSObject] ?? []
let newActions = makeActions(actions) let newActions = try makeActions(actions)
// Create the custom actions for the TIMER_EXPIRED category. // Create the custom actions for the TIMER_EXPIRED category.
var newCategory: UNNotificationCategory? var newCategory: UNNotificationCategory?
@ -38,13 +51,12 @@ public func makeCategories(_ actionTypes: [JSObject]) {
center.setNotificationCategories(Set(createdCategories)) center.setNotificationCategories(Set(createdCategories))
} }
func makeActions(_ actions: [JSObject]) -> [UNNotificationAction] { func makeActions(_ actions: [JSObject]) throws -> [UNNotificationAction] {
var createdActions = [UNNotificationAction]() var createdActions = [UNNotificationAction]()
for action in actions { for action in actions {
guard let id = action["id"] as? String else { guard let id = action["id"] as? String else {
Logger.error("Action must have an id field") throw CategoryError.noActionId
continue
} }
let title = action["title"] as? String ?? "" let title = action["title"] as? String ?? ""
let input = action["input"] as? Bool ?? false let input = action["input"] as? Bool ?? false

@ -39,7 +39,7 @@ func showNotification(invoke: Invoke, notification: JSObject)
do { do {
if let schedule = notification["schedule"] as? JSObject { if let schedule = notification["schedule"] as? JSObject {
try trigger = handleScheduledNotification(invoke, schedule) try trigger = handleScheduledNotification(schedule)
} }
} catch { } catch {
throw ShowNotificationError.create(error) throw ShowNotificationError.create(error)
@ -154,11 +154,11 @@ class NotificationPlugin: Plugin {
}) })
} }
@objc func registerActionTypes(_ invoke: Invoke) { @objc func registerActionTypes(_ invoke: Invoke) throws {
guard let types = invoke.getArray("types", JSObject.self) else { guard let types = invoke.getArray("types", JSObject.self) else {
return return
} }
makeCategories(types) try makeCategories(types)
invoke.resolve() invoke.resolve()
} }

Loading…
Cancel
Save