How can I return customized error in Swift?

Viewed 55

When I use do.. try.. catch.. print(error) for SQL select, error return example:

sqlError(notice: Notice( localizedSeverity: ERROR severity: ERROR code: 42601 message: syntax error at or near "selectt" position: 1 file: scan.l line: 1128 routine: scanner_yyerror ))

Is there any way to just get a message? "syntax error at or near "selectt""

1 Answers

You didn't share the Error definitions, but let us assume it was something like:

struct Notice {
    let severity: Severity
    let code: Int
    let message: String
    ...
}

enum DatabaseError: Error {
    case sqlError(notice: Notice)
}

Then, you might catch the Notice associated with this sqlError like so:

do {
    try foo()
} catch DatabaseError.sqlError(let notice) {
    print(notice.message)                     // syntax error at or near "selectt"
} catch {
    print(error)
}
Related