-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.c
More file actions
123 lines (111 loc) · 4.55 KB
/
error.c
File metadata and controls
123 lines (111 loc) · 4.55 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Ruby9i -- a Ruby library for accessing Oracle9i through the OCI
* Copyright (C) 2003-2004 James Edwin Cain <ruby9i@jimcain.us>
*
* This file is part of the Ruby9i library. This Library is free software;
* you can redistribute it and/or modify it under the terms of the license
* contained in the file LICENCE.txt. If you did not receive a copy of the
* license, please contact the copyright holder.
*/
#include "ruby9i.h"
#define MAX_BUF_SZ 1000
VALUE error_exception(int argc, VALUE *argv, VALUE klass)
{
VALUE err = rb_obj_alloc(eError);
rb_obj_call_init(err, argc, argv);
return err;
}
void error_raise(char *msg, char *mname, const char* fname, int lineno)
{
VALUE argv[] = { rb_str_new2(msg), rb_str_new2(mname), rb_str_new2(fname), INT2FIX(lineno) };
rb_exc_raise(error_exception(4, argv, eError));
}
void error_raise_sql(char *msg, char *mname, char *fname, int lineno, char *sql, int offset)
{
VALUE argv[] = { rb_str_new2(msg), rb_str_new2(mname), rb_str_new2(fname), INT2FIX(lineno),
rb_str_new2(sql), INT2FIX(offset) };
rb_exc_raise(error_exception(6, argv, eError));
}
VALUE error_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE mesg, mname, fname, lineno, sql, offset;
rb_scan_args(argc, argv, "06", &mesg, &mname, &fname, &lineno, &sql, &offset);
rb_call_super(1, &mesg);
rb_iv_set(self, "@methodname", mname);
rb_iv_set(self, "@filename", fname);
rb_iv_set(self, "@lineno", lineno);
rb_iv_set(self, "@sql", sql);
rb_iv_set(self, "@offset", offset);
/* get Oracle error text */
text buf[MAX_BUF_SZ];
sb4 ora_code;
if (OCIErrorGet(err_h, 1, NULL, &ora_code, buf, MAX_BUF_SZ, OCI_HTYPE_ERROR) == 0)
{
rb_iv_set(self, "@oramesg", rb_funcall(rb_str_new(buf, strlen(buf)), rb_intern("chomp!"), 0));
rb_iv_set(self, "@oranum", INT2FIX(ora_code));
}
else
{
rb_iv_set(self, "@oramesg", Qnil);
rb_iv_set(self, "@oranum", INT2FIX(0));
}
rb_define_attr(CLASS_OF(self), "oramesg", 1, 0);
rb_define_attr(CLASS_OF(self), "oranum", 1, 0);
return self;
}
void error_get_sql_errstr(VALUE self, VALUE *errline, VALUE *errptr)
{
VALUE sql = rb_iv_get(self, "@sql");
if (NIL_P(sql))
return;
VALUE offset = rb_iv_get(self,"@offset");
if (NIL_P(offset))
return;
int i, start, length;
for (i = FIX2INT(offset); i >= 0 && RSTRING(sql)->ptr[i] != '\n' && RSTRING(sql)->ptr[i] != '\r'; i--)
/* do nothing */ ;
start = i + 1;
for (i = FIX2INT(offset); i < RSTRING(sql)->len && RSTRING(sql)->ptr[i] != '\n' && RSTRING(sql)->ptr[i] != '\r'; i++)
/* do nothing */ ;
length = i - start;
*errline = rb_funcall(sql, ID_SUBSCRIPT, 2, INT2FIX(start), INT2FIX(length));
*errptr = rb_str_concat(rb_funcall(rb_str_new2(" "), rb_intern("*"), 1, INT2FIX(FIX2INT(offset)-start)), rb_str_new2("^"));
}
VALUE error_to_s(VALUE self)
{
VALUE v_mesg = rb_iv_get(self, "mesg");
VALUE v_mname = rb_iv_get(self, "@methodname");
VALUE v_fname = rb_iv_get(self, "@filename");
VALUE v_lineno = rb_iv_get(self, "@lineno");
VALUE v_oramesg = rb_iv_get(self, "@oramesg");
VALUE v_errstr = Qnil;
VALUE v_errptr = Qnil;
error_get_sql_errstr(self, &v_errstr, &v_errptr);
char str[MAX_BUF_SZ]="Error", mname[MAX_BUF_SZ]="", fname[MAX_BUF_SZ]="", lineno[MAX_BUF_SZ]="", oramesg[MAX_BUF_SZ]="";
if (!NIL_P(v_mesg)) strncpy(str, RSTRING(v_mesg)->ptr, MAX_BUF_SZ);
if (!NIL_P(v_mname)) snprintf(mname, MAX_BUF_SZ - strlen(str), " in \"%s\"", RSTRING(v_mname)->ptr);
strncat(str, mname, MAX_BUF_SZ);
if (!NIL_P(v_fname)) snprintf(fname, MAX_BUF_SZ - strlen(str), " in file \"%s\"", RSTRING(v_fname)->ptr);
strncat(str, fname, MAX_BUF_SZ);
if (!NIL_P(v_lineno)) snprintf(lineno, MAX_BUF_SZ - strlen(str), " at line %d", FIX2INT(v_lineno));
strncat(str, lineno, MAX_BUF_SZ);
if (RTEST(v_errstr) && RTEST(v_errptr))
{
strncat(str, "\n", MAX_BUF_SZ);
strncat(str, RSTRING(v_errstr)->ptr, MAX_BUF_SZ);
strncat(str, "\n", MAX_BUF_SZ);
strncat(str, RSTRING(v_errptr)->ptr, MAX_BUF_SZ);
}
if (!NIL_P(v_oramesg)) snprintf(oramesg, MAX_BUF_SZ - strlen(str), "\n%s", RSTRING(v_oramesg)->ptr);
strncat(str, oramesg, MAX_BUF_SZ);
return rb_str_new2(str);
}
void Init_Error()
{
rb_define_singleton_method(eError, "exception", error_exception, -1);
rb_define_singleton_method(eError, "new", error_exception, -1);
rb_define_method(eError, "initialize", error_initialize, -1);
rb_enable_super(eError, "initialize");
rb_define_method(eError, "message", error_to_s, 0);
rb_define_method(eError, "to_s", error_to_s, 0);
}