Skip to content

Commit 8b26692

Browse files
committed
FIX: throw error when trying to make invalid errors
related to: Oldes/Rebol-issues#1593
1 parent 0e9d145 commit 8b26692

File tree

3 files changed

+92
-13
lines changed

3 files changed

+92
-13
lines changed

src/core/c-error.c

+25-13
Original file line numberDiff line numberDiff line change
@@ -302,23 +302,29 @@ static REBOL_STATE Top_State; // Boot var: holds error state during boot
302302
** to be bound to the error catalog context.
303303
** If the message is not found, return null.
304304
**
305+
** NOTE: throws invalid-arg error if id or type are not found
306+
** throws invalid-error if id or type are not words
307+
**
305308
***********************************************************************/
306309
{
307310
REBSER *frame;
308311
REBVAL *obj1;
309312
REBVAL *obj2;
310313

311-
if (!IS_WORD(&error->type) || !IS_WORD(&error->id)) return 0;
314+
if (IS_NONE(&error->type) || IS_NONE(&error->id))
315+
Trap0(RE_INVALID_ERROR);
312316

313317
// Find the correct error type object in the catalog:
318+
if (!IS_WORD(&error->type)) goto invalid_type;
314319
frame = VAL_OBJ_FRAME(Get_System(SYS_CATALOG, CAT_ERRORS));
315320
obj1 = Find_Word_Value(frame, VAL_WORD_SYM(&error->type));
316-
if (!obj1) return 0;
321+
if (!obj1) goto invalid_type;
317322

318323
// Now find the correct error message for that type:
324+
if (!IS_WORD(&error->id)) goto invalid_id;
319325
frame = VAL_OBJ_FRAME(obj1);
320326
obj2 = Find_Word_Value(frame, VAL_WORD_SYM(&error->id));
321-
if (!obj2) return 0;
327+
if (!obj2) goto invalid_id;
322328

323329
if (num) {
324330
obj1 = Find_Word_Value(frame, SYM_CODE);
@@ -329,6 +335,11 @@ static REBOL_STATE Top_State; // Boot var: holds error state during boot
329335
}
330336

331337
return obj2;
338+
invalid_type:
339+
Trap1(RE_INVALID_ARG, &error->type);
340+
invalid_id:
341+
Trap1(RE_INVALID_ARG, &error->id);
342+
return 0; // just for compiler
332343
}
333344

334345

@@ -371,19 +382,20 @@ static REBOL_STATE Top_State; // Boot var: holds error state during boot
371382
DISABLE_GC;
372383
Do_Bind_Block(err, arg); // GC-OK (disabled)
373384
ENABLE_GC;
374-
if (IS_INTEGER(&error->code) && VAL_INT64(&error->code)) {
375-
Set_Error_Type(error);
376-
} else {
377-
if (Find_Error_Info(error, &code)) {
378-
SET_INTEGER(&error->code, code);
379-
}
380-
}
385+
//It was possible to set error using code, but that's now ignored!
386+
//@@ https://github.com/Oldes/Rebol-issues/issues/1593
387+
//if (IS_INTEGER(&error->code) && VAL_INT64(&error->code)) {
388+
// Set_Error_Type(error);
389+
//} else {
390+
if (!Find_Error_Info(error, &code)) code = RE_INVALID_ERROR;
391+
SET_INTEGER(&error->code, code);
392+
393+
//}
381394
// The error code is not valid:
382-
if (IS_NONE(&error->id)) {
383-
SET_INTEGER(&error->code, RE_INVALID_ERROR);
395+
if (VAL_INT64(&error->code) == RE_INVALID_ERROR) {
384396
Set_Error_Type(error);
385397
}
386-
if (VAL_INT64(&error->code) < 100 || VAL_INT64(&error->code) > 1000)
398+
if (VAL_INT64(&error->code) < 100) // || VAL_INT64(&error->code) > 1000)
387399
Trap_Arg(arg);
388400
}
389401

src/tests/run-tests.r3

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dt [ ;- delta time
2323
%units/delect-test.r3
2424
%units/enbase-test.r3
2525
%units/enum-test.r3
26+
%units/error-test.r3
2627
%units/evaluation-test.r3
2728
%units/event-test.r3
2829
%units/gob-test.r3

src/tests/units/error-test.r3

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Rebol [
2+
Title: "Rebol error test script"
3+
Author: "Oldes"
4+
File: %error-test.r3
5+
Tabs: 4
6+
Needs: [%../quick-test-module.r3]
7+
]
8+
9+
~~~start-file~~~ "ERROR"
10+
11+
===start-group=== "make error!"
12+
;@@ https://github.com/Oldes/Rebol-issues/issues/1593
13+
--test-- "make error!"
14+
k: keys-of system/catalog/errors/Math
15+
--assert all [
16+
error? e: make error! [type: 'math id: 'positive]
17+
e/code = (system/catalog/errors/Math/code - 3 + index? find k 'positive)
18+
e/type = 'Math
19+
e/id = 'positive
20+
]
21+
--assert all [
22+
error? e: make error! [code: 500 type: 'math id: 'overflow] ;- code value 500 ignored!
23+
e/code = (system/catalog/errors/Math/code - 3 + index? find k 'overflow)
24+
e/type = 'Math
25+
e/id = 'overflow
26+
]
27+
28+
--test-- "make invalid error!"
29+
--assert all [ ;missing type and id
30+
error? e: try [make error! []]
31+
e/type = 'Internal
32+
e/id = 'invalid-error
33+
]
34+
--assert all [ ;missing type and id
35+
error? e: try [make error! [code: 400]]
36+
e/type = 'Internal
37+
e/id = 'invalid-error
38+
]
39+
--assert all [ ;missing id
40+
error? e: try [make error! [code: 500 type: 'math]]
41+
e/type = 'Internal
42+
e/id = 'invalid-error
43+
]
44+
--assert all [
45+
error? e: try [make error! [type: 'math id: 'foo]]
46+
e/type = 'Script
47+
e/id = 'invalid-arg
48+
e/arg1 = 'foo
49+
]
50+
--assert all [
51+
error? e: try [make error! [type: 'math id: 42]]
52+
e/type = 'Script
53+
e/id = 'invalid-arg
54+
e/arg1 = 42
55+
]
56+
57+
--assert all [
58+
error? e: try [make error! [type: 'foo id: 'overflow]]
59+
e/type = 'Script
60+
e/id = 'invalid-arg
61+
e/arg1 = 'foo
62+
]
63+
===end-group===
64+
65+
66+
~~~end-file~~~

0 commit comments

Comments
 (0)