Fossil SCM
started adding infrastructure to report non-fatal warnings.
Commit
ad50fe9584dc909503ee88993ac3f2344b19cb8a
Parent
d6cbe37b6b63c91…
2 files changed
+43
+4
+43
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -462,10 +462,45 @@ | ||
| 462 | 462 | g.json.param.v = v; |
| 463 | 463 | g.json.param.o = cson_value_get_object(v); |
| 464 | 464 | json_gc_add("$PARAMS", v, 1); |
| 465 | 465 | } |
| 466 | 466 | |
| 467 | +/* | |
| 468 | +** Appends a warning object to the response. | |
| 469 | +** | |
| 470 | +** TODO: specify what the code must be. | |
| 471 | +** | |
| 472 | +** A Warning object has this JSON structure: | |
| 473 | +** | |
| 474 | +** { "code":integer, "text":"string" } | |
| 475 | +** | |
| 476 | +** But the text part is optional. | |
| 477 | +** | |
| 478 | +** If msg is non-NULL and not empty then it is used | |
| 479 | +** as the "text" property's value. | |
| 480 | +*/ | |
| 481 | +void json_add_warning( int code, char const * msg ){ | |
| 482 | + cson_value * objV = NULL; | |
| 483 | + cson_object * obj = NULL; | |
| 484 | + if(!g.json.warnings.v){ | |
| 485 | + g.json.warnings.v = cson_value_new_array(); | |
| 486 | + assert((NULL != g.json.warnings.v) && "Alloc error."); | |
| 487 | + g.json.warnings.a = cson_value_get_array(g.json.warnings.v); | |
| 488 | + json_gc_add("$WARNINGS",g.json.warnings.v,0); | |
| 489 | + } | |
| 490 | + objV = cson_value_new_object(); | |
| 491 | + assert((NULL != objV) && "Alloc error."); | |
| 492 | + cson_array_append(g.json.warnings.a, objV); | |
| 493 | + obj = cson_value_get_object(objV); | |
| 494 | + cson_object_set(obj,"code",cson_value_new_integer(code)); | |
| 495 | + if(msg && *msg){ | |
| 496 | + /* FIXME: treat NULL msg as standard warning message for | |
| 497 | + the code, but we don't have those yet. | |
| 498 | + */ | |
| 499 | + cson_object_set(obj,"text",cson_value_new_string(msg,strlen(msg))); | |
| 500 | + } | |
| 501 | +} | |
| 467 | 502 | |
| 468 | 503 | /* |
| 469 | 504 | ** Splits zStr (which must not be NULL) into tokens separated by the |
| 470 | 505 | ** given separator character. If doDeHttp is true then each element |
| 471 | 506 | ** will be passed through dehttpize(), otherwise they are used |
| @@ -882,10 +917,15 @@ | ||
| 882 | 917 | cson_object_set( o, "cmd.offset", tmp ); |
| 883 | 918 | cson_object_set( o, "isCGI", cson_value_new_bool( g.isHTTP ) ); |
| 884 | 919 | } |
| 885 | 920 | } |
| 886 | 921 | |
| 922 | + if(g.json.warnings.v){ | |
| 923 | + tmp = g.json.warnings.v; | |
| 924 | + SET("warnings"); | |
| 925 | + } | |
| 926 | + | |
| 887 | 927 | /* Only add the payload to SUCCESS responses. Else delete it. */ |
| 888 | 928 | if( NULL != payload ){ |
| 889 | 929 | if( resultCode ){ |
| 890 | 930 | cson_value_free(payload); |
| 891 | 931 | payload = NULL; |
| @@ -1746,10 +1786,13 @@ | ||
| 1746 | 1786 | json_mode_bootstrap(); |
| 1747 | 1787 | if( g.argc<3 ){ |
| 1748 | 1788 | goto usage; |
| 1749 | 1789 | } |
| 1750 | 1790 | db_find_and_open_repository(0, 0); |
| 1791 | +#if 0 | |
| 1792 | + json_add_warning(-1, "Just testing."); | |
| 1793 | +#endif | |
| 1751 | 1794 | cmd = json_command_arg(1); |
| 1752 | 1795 | if( !cmd || !*cmd ){ |
| 1753 | 1796 | goto usage; |
| 1754 | 1797 | } |
| 1755 | 1798 | pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); |
| 1756 | 1799 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -462,10 +462,45 @@ | |
| 462 | g.json.param.v = v; |
| 463 | g.json.param.o = cson_value_get_object(v); |
| 464 | json_gc_add("$PARAMS", v, 1); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* |
| 469 | ** Splits zStr (which must not be NULL) into tokens separated by the |
| 470 | ** given separator character. If doDeHttp is true then each element |
| 471 | ** will be passed through dehttpize(), otherwise they are used |
| @@ -882,10 +917,15 @@ | |
| 882 | cson_object_set( o, "cmd.offset", tmp ); |
| 883 | cson_object_set( o, "isCGI", cson_value_new_bool( g.isHTTP ) ); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /* Only add the payload to SUCCESS responses. Else delete it. */ |
| 888 | if( NULL != payload ){ |
| 889 | if( resultCode ){ |
| 890 | cson_value_free(payload); |
| 891 | payload = NULL; |
| @@ -1746,10 +1786,13 @@ | |
| 1746 | json_mode_bootstrap(); |
| 1747 | if( g.argc<3 ){ |
| 1748 | goto usage; |
| 1749 | } |
| 1750 | db_find_and_open_repository(0, 0); |
| 1751 | cmd = json_command_arg(1); |
| 1752 | if( !cmd || !*cmd ){ |
| 1753 | goto usage; |
| 1754 | } |
| 1755 | pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); |
| 1756 |
| --- src/json.c | |
| +++ src/json.c | |
| @@ -462,10 +462,45 @@ | |
| 462 | g.json.param.v = v; |
| 463 | g.json.param.o = cson_value_get_object(v); |
| 464 | json_gc_add("$PARAMS", v, 1); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | ** Appends a warning object to the response. |
| 469 | ** |
| 470 | ** TODO: specify what the code must be. |
| 471 | ** |
| 472 | ** A Warning object has this JSON structure: |
| 473 | ** |
| 474 | ** { "code":integer, "text":"string" } |
| 475 | ** |
| 476 | ** But the text part is optional. |
| 477 | ** |
| 478 | ** If msg is non-NULL and not empty then it is used |
| 479 | ** as the "text" property's value. |
| 480 | */ |
| 481 | void json_add_warning( int code, char const * msg ){ |
| 482 | cson_value * objV = NULL; |
| 483 | cson_object * obj = NULL; |
| 484 | if(!g.json.warnings.v){ |
| 485 | g.json.warnings.v = cson_value_new_array(); |
| 486 | assert((NULL != g.json.warnings.v) && "Alloc error."); |
| 487 | g.json.warnings.a = cson_value_get_array(g.json.warnings.v); |
| 488 | json_gc_add("$WARNINGS",g.json.warnings.v,0); |
| 489 | } |
| 490 | objV = cson_value_new_object(); |
| 491 | assert((NULL != objV) && "Alloc error."); |
| 492 | cson_array_append(g.json.warnings.a, objV); |
| 493 | obj = cson_value_get_object(objV); |
| 494 | cson_object_set(obj,"code",cson_value_new_integer(code)); |
| 495 | if(msg && *msg){ |
| 496 | /* FIXME: treat NULL msg as standard warning message for |
| 497 | the code, but we don't have those yet. |
| 498 | */ |
| 499 | cson_object_set(obj,"text",cson_value_new_string(msg,strlen(msg))); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | ** Splits zStr (which must not be NULL) into tokens separated by the |
| 505 | ** given separator character. If doDeHttp is true then each element |
| 506 | ** will be passed through dehttpize(), otherwise they are used |
| @@ -882,10 +917,15 @@ | |
| 917 | cson_object_set( o, "cmd.offset", tmp ); |
| 918 | cson_object_set( o, "isCGI", cson_value_new_bool( g.isHTTP ) ); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if(g.json.warnings.v){ |
| 923 | tmp = g.json.warnings.v; |
| 924 | SET("warnings"); |
| 925 | } |
| 926 | |
| 927 | /* Only add the payload to SUCCESS responses. Else delete it. */ |
| 928 | if( NULL != payload ){ |
| 929 | if( resultCode ){ |
| 930 | cson_value_free(payload); |
| 931 | payload = NULL; |
| @@ -1746,10 +1786,13 @@ | |
| 1786 | json_mode_bootstrap(); |
| 1787 | if( g.argc<3 ){ |
| 1788 | goto usage; |
| 1789 | } |
| 1790 | db_find_and_open_repository(0, 0); |
| 1791 | #if 0 |
| 1792 | json_add_warning(-1, "Just testing."); |
| 1793 | #endif |
| 1794 | cmd = json_command_arg(1); |
| 1795 | if( !cmd || !*cmd ){ |
| 1796 | goto usage; |
| 1797 | } |
| 1798 | pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]); |
| 1799 |
+4
| --- src/main.c | ||
| +++ src/main.c | ||
| @@ -207,10 +207,14 @@ | ||
| 207 | 207 | } param; |
| 208 | 208 | struct { |
| 209 | 209 | cson_value * v; |
| 210 | 210 | cson_object * o; |
| 211 | 211 | } reqPayload; /* request payload object (if any) */ |
| 212 | + struct { /* response warnings */ | |
| 213 | + cson_value * v; | |
| 214 | + cson_array * a; | |
| 215 | + } warnings; | |
| 212 | 216 | } json; |
| 213 | 217 | }; |
| 214 | 218 | |
| 215 | 219 | /* |
| 216 | 220 | ** Macro for debugging: |
| 217 | 221 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -207,10 +207,14 @@ | |
| 207 | } param; |
| 208 | struct { |
| 209 | cson_value * v; |
| 210 | cson_object * o; |
| 211 | } reqPayload; /* request payload object (if any) */ |
| 212 | } json; |
| 213 | }; |
| 214 | |
| 215 | /* |
| 216 | ** Macro for debugging: |
| 217 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -207,10 +207,14 @@ | |
| 207 | } param; |
| 208 | struct { |
| 209 | cson_value * v; |
| 210 | cson_object * o; |
| 211 | } reqPayload; /* request payload object (if any) */ |
| 212 | struct { /* response warnings */ |
| 213 | cson_value * v; |
| 214 | cson_array * a; |
| 215 | } warnings; |
| 216 | } json; |
| 217 | }; |
| 218 | |
| 219 | /* |
| 220 | ** Macro for debugging: |
| 221 |