Kitura / Kitura/SwiftKueryMySQL
Original MySQL error message is lost when executing a query
- Dominant language
- Swift
- Stars
- 34
- Forks
- 13
- PR merge metrics
- No merged PRs in 30d
Description
**Problem**
Hello... Unless I'm missing something, when executing a query, the returned error loses the original message generated by MySQL.
**Example**
These 3 statements return the same generic error description:
```mysql
SELEC * FROM DUAL
LOCK TABLE some_table READ
SELECT * FROM non_existent_table
```
In all cases, ```QueryError.description``` returns the same message:
```
The operation couldn’t be completed. (SwiftKuery.QueryError error 2.)
```
However, their respective errors returned by MySQL are:
```
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELEC * FROM DUAL' at line 1
ERROR 1295: This command is not supported in the prepared statement protocol yet
ERROR 1146: Table 'myschema.non_existent_table' doesn't exist
```
**Proposed Solution**
A simple change in the _MySQLConnection.execute(...)_ functions solves the problem. By replacing:
https://github.com/IBM-Swift/SwiftKueryMySQL/blob/27e631c8a5dfff19561423324f7881fb58246455/Sources/SwiftKueryMySQL/MySQLConnection.swift#L246
and also
https://github.com/IBM-Swift/SwiftKueryMySQL/blob/27e631c8a5dfff19561423324f7881fb58246455/Sources/SwiftKueryMySQL/MySQLConnection.swift#L272
with the following code:
```swift
if let error = error as? QueryError {
return self.runCompletionHandler(.error(error), onCompletion: onCompletion)
} else {
return self.runCompletionHandler(.error(QueryError.databaseError(error.localizedDescription)), onCompletion: onCompletion)
}
```
There may be other places where this needs to be done, but these are the most important.
**Final Comments**
Maybe this is intended behavior? However, during the development of a project, not having this information is really annoying.
At the moment, when we realize an error has occurred, we need to repeat the actions that took us to execute such query, use the debugger to pause execution, and get to the line of the library where the original error message is discarded. Not cool!
It would be nice to be able to log the real cause of the problem, so we can catch the problem the first time around the query fails.
Note that with the proposed change, the generic error _The operation couldn’t be completed. (SwiftKuery.QueryError error 2.)_ is still accessible, using ```.localizedDescription``` instead of ```.description```
If still, the current behavior is mandatory, couldn't we have a debug mode option? For example, some static variable in MySQLConnection that makes the library to return the original mysql messages, instead of the generic one: For example:
```swift
MySQLConnection.debug_mode = true
```
Thank you for your time!
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.