Skip to content

Commit 8ac6db6

Browse files
(capi) - Add more declarative errors
1 parent f97008d commit 8ac6db6

File tree

11 files changed

+144
-90
lines changed

11 files changed

+144
-90
lines changed

rayforce/capi/rayforce_c.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ static unsigned long g_main_thread_id = 0; // main thread ID
66

77
int check_main_thread(void) {
88
if (g_main_thread_id == 0) {
9-
PyErr_SetString(PyExc_RuntimeError, "Runtime not initialized.");
9+
PyErr_SetString(PyExc_RuntimeError, "runtime: not initialized");
1010
return 0;
1111
}
1212

1313
if ((unsigned long)PyThread_get_thread_ident() != g_main_thread_id) {
14-
PyErr_Format(PyExc_RuntimeError,
15-
"Rayforce runtime can not be called from other threads than "
16-
"the one where it was initialized from");
14+
PyErr_SetString(PyExc_RuntimeError,
15+
"runtime: cannot be called from threads other than the "
16+
"initialization thread");
1717
return 0;
1818
}
1919

@@ -26,13 +26,14 @@ PyObject *raypy_init_runtime(PyObject *self, PyObject *args) {
2626
(void)args;
2727

2828
if (g_runtime != NULL) {
29-
PyErr_SetString(PyExc_RuntimeError, "Runtime already initialized");
29+
PyErr_SetString(PyExc_RuntimeError, "runtime: already initialized");
3030
return NULL;
3131
}
3232

3333
char *argv[] = {"py", "-r", "0", NULL};
3434
if (runtime_create(3, argv) == NULL) {
35-
PyErr_SetString(PyExc_RuntimeError, "Failed to initialize Rayforce");
35+
PyErr_SetString(PyExc_RuntimeError,
36+
"runtime: failed to initialize Rayforce");
3637
return NULL;
3738
}
3839

@@ -41,7 +42,7 @@ PyObject *raypy_init_runtime(PyObject *self, PyObject *args) {
4142
}
4243
PyObject *raypy_wrap_ray_object(obj_p ray_obj) {
4344
if (ray_obj == NULL) {
44-
PyErr_SetString(PyExc_RuntimeError, "Rayforce object can't be null");
45+
PyErr_SetString(PyExc_RuntimeError, "runtime: object cannot be null");
4546
return NULL;
4647
}
4748

rayforce/capi/raypy_binary.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ PyObject *raypy_binary_set(PyObject *self, PyObject *args) {
1212

1313
if (symbol_or_path->obj->type != -TYPE_SYMBOL &&
1414
symbol_or_path->obj->type != TYPE_C8) {
15-
PyErr_SetString(PyExc_TypeError,
16-
"First argument must be a symbol or string");
15+
PyErr_SetString(PyExc_RuntimeError,
16+
"binary: first argument must be a symbol or string");
1717
return NULL;
1818
}
1919

2020
obj_p ray_obj = binary_set(symbol_or_path->obj, value->obj);
2121
if (ray_obj == NULL) {
22-
PyErr_SetString(PyExc_RuntimeError, "Failed to execute set operation");
22+
PyErr_SetString(PyExc_RuntimeError, "binary: failed to set value");
2323
return NULL;
2424
}
2525
return raypy_wrap_ray_object(ray_obj);
@@ -46,7 +46,7 @@ PyObject *raypy_quote(PyObject *self, PyObject *args) {
4646

4747
obj_p ray_obj = ray_quote(item->obj);
4848
if (ray_obj == NULL) {
49-
PyErr_SetString(PyExc_RuntimeError, "Failed to execute quote operation");
49+
PyErr_SetString(PyExc_RuntimeError, "binary: failed to quote object");
5050
return NULL;
5151
}
5252
return raypy_wrap_ray_object(ray_obj);

rayforce/capi/raypy_dynlib.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ PyObject *raypy_loadfn(PyObject *self, PyObject *args) {
1414

1515
obj_p path_obj = vector(TYPE_C8, path_len);
1616
if (path_obj == NULL) {
17-
PyErr_SetString(PyExc_MemoryError, "Failed to allocate path object");
17+
PyErr_SetString(PyExc_RuntimeError,
18+
"dynlib: failed to allocate path object");
1819
return NULL;
1920
}
2021
memcpy(AS_C8(path_obj), path, path_len);
2122

2223
obj_p func_obj = vector(TYPE_C8, func_len);
2324
if (func_obj == NULL) {
2425
drop_obj(path_obj);
25-
PyErr_SetString(PyExc_MemoryError, "Failed to allocate fn name object");
26+
PyErr_SetString(PyExc_RuntimeError,
27+
"dynlib: failed to allocate function name object");
2628
return NULL;
2729
}
2830
memcpy(AS_C8(func_obj), func_name, func_len);
@@ -31,7 +33,8 @@ PyObject *raypy_loadfn(PyObject *self, PyObject *args) {
3133
if (nargs_obj == NULL) {
3234
drop_obj(path_obj);
3335
drop_obj(func_obj);
34-
PyErr_SetString(PyExc_MemoryError, "Failed to allocate nargs object");
36+
PyErr_SetString(PyExc_RuntimeError,
37+
"dynlib: failed to allocate nargs object");
3538
return NULL;
3639
}
3740

@@ -41,7 +44,8 @@ PyObject *raypy_loadfn(PyObject *self, PyObject *args) {
4144
drop_obj(nargs_obj);
4245

4346
if (ray_obj == NULL) {
44-
PyErr_SetString(PyExc_RuntimeError, "Failed load fn from shared library");
47+
PyErr_SetString(PyExc_RuntimeError,
48+
"dynlib: failed to load function from shared library");
4549
return NULL;
4650
}
4751
return raypy_wrap_ray_object(ray_obj);

rayforce/capi/raypy_eval.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ PyObject *raypy_eval_str(PyObject *self, PyObject *args) {
1010

1111
obj_p ray_obj = ray_eval_str(item->obj, NULL_OBJ);
1212
if (ray_obj == NULL) {
13-
PyErr_SetString(PyExc_RuntimeError, "Failed to evaluate expression");
13+
PyErr_SetString(PyExc_RuntimeError,
14+
"eval: failed to evaluate string expression");
1415
return NULL;
1516
}
1617
return raypy_wrap_ray_object(ray_obj);
@@ -25,7 +26,7 @@ PyObject *raypy_eval_obj(PyObject *self, PyObject *args) {
2526

2627
obj_p ray_obj = eval_obj(item->obj);
2728
if (ray_obj == NULL) {
28-
PyErr_SetString(PyExc_RuntimeError, "Failed to evaluate expression");
29+
PyErr_SetString(PyExc_RuntimeError, "eval: failed to evaluate object");
2930
return NULL;
3031
}
3132
return raypy_wrap_ray_object(ray_obj);

0 commit comments

Comments
 (0)