Fossil SCM

started adding infrastructure to report non-fatal warnings.

stephan 2011-09-21 16:31 json
Commit ad50fe9584dc909503ee88993ac3f2344b19cb8a
2 files changed +43 +4
+43
--- src/json.c
+++ src/json.c
@@ -462,10 +462,45 @@
462462
g.json.param.v = v;
463463
g.json.param.o = cson_value_get_object(v);
464464
json_gc_add("$PARAMS", v, 1);
465465
}
466466
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
+}
467502
468503
/*
469504
** Splits zStr (which must not be NULL) into tokens separated by the
470505
** given separator character. If doDeHttp is true then each element
471506
** will be passed through dehttpize(), otherwise they are used
@@ -882,10 +917,15 @@
882917
cson_object_set( o, "cmd.offset", tmp );
883918
cson_object_set( o, "isCGI", cson_value_new_bool( g.isHTTP ) );
884919
}
885920
}
886921
922
+ if(g.json.warnings.v){
923
+ tmp = g.json.warnings.v;
924
+ SET("warnings");
925
+ }
926
+
887927
/* Only add the payload to SUCCESS responses. Else delete it. */
888928
if( NULL != payload ){
889929
if( resultCode ){
890930
cson_value_free(payload);
891931
payload = NULL;
@@ -1746,10 +1786,13 @@
17461786
json_mode_bootstrap();
17471787
if( g.argc<3 ){
17481788
goto usage;
17491789
}
17501790
db_find_and_open_repository(0, 0);
1791
+#if 0
1792
+ json_add_warning(-1, "Just testing.");
1793
+#endif
17511794
cmd = json_command_arg(1);
17521795
if( !cmd || !*cmd ){
17531796
goto usage;
17541797
}
17551798
pageDef = json_handler_for_name(cmd,&JsonPageDefs[0]);
17561799
--- 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 @@
207207
} param;
208208
struct {
209209
cson_value * v;
210210
cson_object * o;
211211
} reqPayload; /* request payload object (if any) */
212
+ struct { /* response warnings */
213
+ cson_value * v;
214
+ cson_array * a;
215
+ } warnings;
212216
} json;
213217
};
214218
215219
/*
216220
** Macro for debugging:
217221
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button