@@ -47,11 +47,10 @@ function make_ctx($preset, $method = "POST") {
4747 return $ ctx ;
4848}
4949
50- function failed ($ result ) {
51- echo "\n\n" ;
52- echo join ("\n" , $ result );
53- printf ("Last operation took: %.2f secs \n" , lap ());
54- exit ();
50+ function failed ()
51+ {
52+ printf ("\nLast operation took: %.2f secs \n" , lap ());
53+ exit (1 );
5554}
5655
5756function mo_http_request ($ uri , $ context ) {
@@ -60,17 +59,32 @@ function mo_http_request($uri, $context) {
6059 $ result = file_get_contents ($ uri , false , $ context );
6160
6261 if ($ result === false ) {
63- failed ($ http_response_header );
62+ printf ("HTTP request to %s failed: \n" , $ uri );
63+ var_dump ($ http_response_header );
64+ failed ();
6465 }
6566
6667 return $ result ;
6768}
6869
70+ function json_decode_or_fail (...$ args )
71+ {
72+ $ decoded = json_decode (...$ args );
73+
74+ if ($ decoded === NULL && json_last_error () !== JSON_ERROR_NONE ) {
75+ printf ("\njson_decode() failed: %s \n" , json_last_error_msg ());
76+ var_dump (func_get_arg (0 ));
77+ failed ();
78+ }
79+
80+ return $ decoded ;
81+ }
82+
6983printf ("Cleaning out previous processes, if any " );
7084lap ();
7185/* Remove all pre-existing ReplicaSets */
7286$ replicasets = mo_http_request (getMOUri () . "/replica_sets " , make_ctx (getMOPresetBase (), "GET " ));
73- $ replicasets = json_decode ($ replicasets , true );
87+ $ replicasets = json_decode_or_fail ($ replicasets , true );
7488foreach ($ replicasets ["replica_sets " ] as $ replicaset ) {
7589 $ uri = getMOUri () . "/replica_sets/ " . $ replicaset ["id " ];
7690 mo_http_request ($ uri , make_ctx (getMOPresetBase (), "DELETE " ));
@@ -79,7 +93,7 @@ function mo_http_request($uri, $context) {
7993echo " " ;
8094/* Remove all pre-existing servers */
8195$ servers = mo_http_request (getMOUri () . "/servers " , make_ctx (getMOPresetBase (), "GET " ));
82- $ servers = json_decode ($ servers , true );
96+ $ servers = json_decode_or_fail ($ servers , true );
8397foreach ($ servers ["servers " ] as $ server ) {
8498 $ uri = getMOUri () . "/servers/ " . $ server ["id " ];
8599 mo_http_request ($ uri , make_ctx (getMOPresetBase (), "DELETE " ));
@@ -89,13 +103,15 @@ function mo_http_request($uri, $context) {
89103
90104foreach ($ PRESETS ["standalone " ] as $ preset ) {
91105 lap ();
92- $ json = json_decode (file_get_contents ($ preset ), true );
106+ $ json = json_decode_or_fail (file_get_contents ($ preset ), true );
93107 printf ("Starting %-20s ... " , $ json ["id " ]);
94108
95109 $ result = mo_http_request (getMOUri () . "/servers " , make_ctx (getMOPresetBase () . $ preset ));
96- $ decode = json_decode ($ result , true );
110+ $ decode = json_decode_or_fail ($ result , true );
111+
97112 if (!isset ($ decode ["id " ])) {
98- failed ($ decode );
113+ printf ("\"id \" field not found in server response: \n%s \n" , $ decode );
114+ failed ();
99115 }
100116
101117 $ SERVERS [$ decode ["id " ]] = isset ($ decode ["mongodb_auth_uri " ]) ? $ decode ["mongodb_auth_uri " ] : $ decode ["mongodb_uri " ];
@@ -105,14 +121,17 @@ function mo_http_request($uri, $context) {
105121
106122foreach ($ PRESETS ["replicasets " ] as $ preset ) {
107123 lap ();
108- $ json = json_decode (file_get_contents ($ preset ), true );
124+ $ json = json_decode_or_fail (file_get_contents ($ preset ), true );
109125 printf ("Starting %-20s ... " , $ json ["id " ]);
110126
111127 $ result = mo_http_request (getMOUri () . "/replica_sets " , make_ctx (getMOPresetBase () . $ preset ));
112- $ decode = json_decode ($ result , true );
128+ $ decode = json_decode_or_fail ($ result , true );
129+
113130 if (!isset ($ decode ["id " ])) {
114- failed ($ decode );
131+ printf ("\"id \" field not found in replica set response: \n%s \n" , $ decode );
132+ failed ();
115133 }
134+
116135 $ SERVERS [$ decode ["id " ]] = isset ($ decode ["mongodb_auth_uri " ]) ? $ decode ["mongodb_auth_uri " ] : $ decode ["mongodb_uri " ];
117136 printf ("'%s' \t(took: %.2f secs) \n" , $ SERVERS [$ decode ["id " ]], lap ());
118137}
0 commit comments