@@ -163,8 +163,12 @@ public function createConnection($uri)
163
163
}
164
164
165
165
$ parts = parse_url ($ uri );
166
+ $ uri = preg_replace ('#:[^:/]*@# ' , ':***@ ' , $ uri );
166
167
if (!isset ($ parts ['scheme ' ], $ parts ['host ' ]) || $ parts ['scheme ' ] !== 'mysql ' ) {
167
- return \React \Promise \reject (new \InvalidArgumentException ('Invalid connect uri given ' ));
168
+ return \React \Promise \reject (new \InvalidArgumentException (
169
+ 'Invalid MySQL URI given (EINVAL) ' ,
170
+ \defined ('SOCKET_EINVAL ' ) ? \SOCKET_EINVAL : 22
171
+ ));
168
172
}
169
173
170
174
$ args = [];
@@ -187,9 +191,12 @@ public function createConnection($uri)
187
191
$ parts ['host ' ] . ': ' . (isset ($ parts ['port ' ]) ? $ parts ['port ' ] : 3306 )
188
192
);
189
193
190
- $ deferred = new Deferred (function ($ _ , $ reject ) use ($ connecting ) {
194
+ $ deferred = new Deferred (function ($ _ , $ reject ) use ($ connecting, $ uri ) {
191
195
// connection cancelled, start with rejecting attempt, then clean up
192
- $ reject (new \RuntimeException ('Connection to database server cancelled ' ));
196
+ $ reject (new \RuntimeException (
197
+ 'Connection to ' . $ uri . ' cancelled (ECONNABORTED) ' ,
198
+ \defined ('SOCKET_ECONNABORTED ' ) ? \SOCKET_ECONNABORTED : 103
199
+ ));
193
200
194
201
// either close successful connection or cancel pending connection attempt
195
202
$ connecting ->then (function (SocketConnectionInterface $ connection ) {
@@ -198,7 +205,7 @@ public function createConnection($uri)
198
205
$ connecting ->cancel ();
199
206
});
200
207
201
- $ connecting ->then (function (SocketConnectionInterface $ stream ) use ($ authCommand , $ deferred ) {
208
+ $ connecting ->then (function (SocketConnectionInterface $ stream ) use ($ authCommand , $ deferred, $ uri ) {
202
209
$ executor = new Executor ();
203
210
$ parser = new Parser ($ stream , $ executor );
204
211
@@ -209,12 +216,27 @@ public function createConnection($uri)
209
216
$ command ->on ('success ' , function () use ($ deferred , $ connection ) {
210
217
$ deferred ->resolve ($ connection );
211
218
});
212
- $ command ->on ('error ' , function ($ error ) use ($ deferred , $ stream ) {
213
- $ deferred ->reject ($ error );
219
+ $ command ->on ('error ' , function (\Exception $ error ) use ($ deferred , $ stream , $ uri ) {
220
+ $ const = '' ;
221
+ $ errno = $ error ->getCode ();
222
+ if ($ error instanceof Exception) {
223
+ $ const = ' (EACCES) ' ;
224
+ $ errno = \defined ('SOCKET_EACCES ' ) ? \SOCKET_EACCES : 13 ;
225
+ }
226
+
227
+ $ deferred ->reject (new \RuntimeException (
228
+ 'Connection to ' . $ uri . ' failed during authentication: ' . $ error ->getMessage () . $ const ,
229
+ $ errno ,
230
+ $ error
231
+ ));
214
232
$ stream ->close ();
215
233
});
216
- }, function ($ error ) use ($ deferred ) {
217
- $ deferred ->reject (new \RuntimeException ('Unable to connect to database server ' , 0 , $ error ));
234
+ }, function (\Exception $ error ) use ($ deferred , $ uri ) {
235
+ $ deferred ->reject (new \RuntimeException (
236
+ 'Connection to ' . $ uri . ' failed: ' . $ error ->getMessage (),
237
+ $ error ->getCode (),
238
+ $ error
239
+ ));
218
240
});
219
241
220
242
// use timeout from explicit ?timeout=x parameter or default to PHP's default_socket_timeout (60)
@@ -223,10 +245,11 @@ public function createConnection($uri)
223
245
return $ deferred ->promise ();
224
246
}
225
247
226
- return \React \Promise \Timer \timeout ($ deferred ->promise (), $ timeout , $ this ->loop )->then (null , function ($ e ) {
248
+ return \React \Promise \Timer \timeout ($ deferred ->promise (), $ timeout , $ this ->loop )->then (null , function ($ e ) use ( $ uri ) {
227
249
if ($ e instanceof TimeoutException) {
228
250
throw new \RuntimeException (
229
- 'Connection to database server timed out after ' . $ e ->getTimeout () . ' seconds '
251
+ 'Connection to ' . $ uri . ' timed out after ' . $ e ->getTimeout () . ' seconds (ETIMEDOUT) ' ,
252
+ \defined ('SOCKET_ETIMEDOUT ' ) ? \SOCKET_ETIMEDOUT : 110
230
253
);
231
254
}
232
255
throw $ e ;
0 commit comments