Handle errors and server messages¶
tdslite doesn't throw exceptions. connect, execute_query and execute_rpc
return a value that tells whether they worked, and SQL Server's own messages,
the reasons for failures among them, go to an info callback.
Receive SQL Server's messages¶
Register an info callback before connecting, so you also see why a login fails:
// Called for each message that SQL Server sends, errors included
static void print_message(void *, const tdsl::tds_info_token & token) noexcept {
Serial.print(token.is_error() ? "server error: " : "server info: ");
// The message text is UTF-16; this prints its ASCII characters
for (const char16_t c : token.msgtext) {
Serial.print(static_cast<char>(c));
}
Serial.println();
}
driver.set_info_callback(print_message);
token.number is SQL Server's error number (e.g. 18456 for a failed login,
208 for an unknown table), and token.is_error() tells an error from an
informational message.
Check the connection¶
const auto cr = driver.connect(params);
if (not(cr == decltype(driver)::e_driver_error_code::success)) {
// connection_failed: the server could not be reached
// login_failed: the server refused the login (see the info callback)
// connection_param_*: a connection parameter is invalid
}
Check a query¶
The result of execute_query converts to false when the query failed. Its
flags tell why:
auto result = driver.execute_query("SELECT * FROM fruits", print_row);
if (not result) {
if (result.request_too_large) {
// The query doesn't fit into the network buffer. Nothing was sent.
} else if (result.response_too_large) {
// A row, the column list or a message is larger than the network
// buffer. The query ran. tdslite closed the connection: call
// driver.connect(...) again, and select less.
} else if (result.network_error) {
// The connection is lost (or timed out), and closed. Call
// driver.connect(...) again.
} else if (result.response_error) {
// The response could not be processed (e.g. unsupported column
// type, out of memory). The result set might be incomplete.
} else {
// SQL Server reported an error. Its message went to the info
// callback.
}
}
For execute_rpc, check result.error() instead; see
Run parameterized queries.
Errors inside stored procedures
In a stored procedure with SET NOCOUNT ON, an error that doesn't end
the procedure (e.g. RAISERROR, or a failed INSERT) reaches the info
callback, but the result reports success. Count the errors in the info
callback to catch them; see
Call a stored procedure.
Recover¶
| What happened | The connection | What to do |
|---|---|---|
| SQL Server reported an error | still usable | Fix the query and carry on. |
request_too_large |
still usable | Shorten the query, or enlarge the network buffer. |
response_error |
still usable | Avoid the column type, or free some memory. |
network_error |
closed | Call driver.connect(params) again. |
response_too_large (with network_error) |
closed | Reconnect. Select less per row, or enlarge the network buffer: the same query fails the same way. |
A reconnect starts a new session: temporary (#) tables of the old session
are gone, and an open transaction of the old session isn't carried over.
A small pattern for long-running devices:
void loop() {
auto result = driver.execute_query(TDSL_PMEMSTR("SELECT ..."), on_row);
if (result.network_error) {
while (not(driver.connect(params) == decltype(driver)::e_driver_error_code::success)) {
delay(5000);
}
}
delay(1000);
}
connect itself makes params.conn_retry_count attempts, waiting
params.conn_retry_delay_ms between them. It blocks the whole time: with
the defaults and an unreachable server, that can be minutes. See
how long connect blocks.
Related¶
- Results and error codes: every value these calls return.
- Troubleshoot: what common errors mean.
- Example sketch: 04 - Read info/error messages