summaryrefslogtreecommitdiff
path: root/libodbcpp/error.cpp
blob: 3d55fc27444ec004531cb3c0ea2dd7a6c8e09541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <malloc.h>
#include "error.h"

static
void
odbc_verror(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * actionfmt, va_list ap, char ** msg)
{
    SQLCHAR     sqlstatus[6];
    SQLINTEGER  sqlerr;
    SQLCHAR     sqlerrmsg[12800];

    char * action;
	if (vasprintf(&action, actionfmt, ap) < 0) {
        syslog(LOG_WARNING, "%s: %d: %ld: %5.5s: \"%s\" : failed to malloc for vasprintf",
                __FUNCTION__, err, sqlerr, sqlstatus, sqlerrmsg);
		return;
	}

    SQLRETURN rc = SQLGetDiagRec(handletype, handle, 1, sqlstatus, &sqlerr, sqlerrmsg,
			sizeof(sqlerrmsg), NULL);
    switch (rc) {
    case SQL_SUCCESS:
    case SQL_SUCCESS_WITH_INFO:
		if (msg) {
			if (asprintf(msg, "%d: %ld: %5.5s: \"%s\" while attempting to %s",
					err, sqlerr, sqlstatus, sqlerrmsg, action) < 1) {
				*msg = NULL;
			}
		}
        syslog(LOG_WARNING, "%s: %d: %ld: %5.5s: \"%s\" while attempting to %s",
                __FUNCTION__, err, sqlerr, sqlstatus, sqlerrmsg, action);
        break;

    case SQL_INVALID_HANDLE:
		if (msg) {
			if (asprintf(msg, "(%d) Invalid handle passed into function trying to %s.",
					err, action) < 1) {
				*msg = NULL;
			}
		}
        syslog(LOG_ERR, "%s: (%d) Invalid handle passed into function trying to %s.",
                __FUNCTION__, err, action);
        break;

    case SQL_NO_DATA:
		if (msg) {
			if (asprintf(msg, "(%d) No error data available for record trying to %s.",
					err, action) < 1) {
				*msg = NULL;
			}
		}
        syslog(LOG_ERR, "%s: (%d) No error data available for record trying to %s.",
                __FUNCTION__, err, action);
        break;

    case SQL_ERROR:
    default:
        syslog(LOG_ERR, "%s: Unexpected error!!", __FUNCTION__);
        break;
    }
    free(action);
}

ODBC::Error::Error(RETCODE err, SQLSMALLINT handletype, SQLHANDLE handle, char const * action, ...)
{
    va_list ap;

    va_start(ap, action);
    	odbc_verror(err, handletype, handle, action, ap, &msg);
	va_end(ap);
}

ODBC::Error::Error(char const * action, ...)
{
    va_list ap;

    va_start(ap, action);
	vsyslog(LOG_ERR, action, ap);
	if (vasprintf(&msg, action, ap) < 1) {
		msg = NULL;
	}
    va_end(ap);
}

ODBC::Error::~Error() throw()
{
	free(msg);
}

const char *
ODBC::Error::what() const throw()
{
	return msg;
}