Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration error does not get caught #3044

Closed
1 task
Riplar opened this issue Jun 4, 2019 · 4 comments · Fixed by loopbackio/loopback-datasource-juggler#1756
Closed
1 task

Migration error does not get caught #3044

Riplar opened this issue Jun 4, 2019 · 4 comments · Fixed by loopbackio/loopback-datasource-juggler#1756
Assignees
Labels
bug developer-experience Issues affecting ease of use and overall experience of LB users

Comments

@Riplar
Copy link

Riplar commented Jun 4, 2019

Steps to reproduce

  1. Checkout todo example
  2. Add postgresql (or any other database) datasource with wrong credentials so that you are unable to connect to the database
  3. Inject the postgresl datasource into the TodoRepository
  4. Run npm run migrate

Current Behavior

The callback function in the catch block in migrate.ts, which should handle the error, does not get called. I am talking about this piece of code:
migrate(process.argv).catch(err => { console.error('Cannot migrate database schema', err); process.exit(1); });

Expected Behavior

In the example the message "Cannot migrate database schema" should appear in the console. In my production application I want to write the error messages in a logfile when you cannot connect to the database.

Acceptance Criteria

  • Make sure the migration error is caught when something goes wrong and it logs the error.
@sbacem
Copy link

sbacem commented Jun 4, 2019

@Riplar
Copy link
Author

Riplar commented Jun 4, 2019

you can write your log like this

https://github.com/strongloop/loopback-next/tree/master/examples/log-extension

@sbacem
This extension is for logging controller methods, which is not my problem. My problem is that I need to detect whether or not I am able to access the database when running the migrate script.

@bajtos
Copy link
Member

bajtos commented Jun 18, 2019

I was able to reproduce the problem using MySQL connector.

$ npm run migrate

> @loopback/[email protected] migrate /Users/bajtos/src/loopback/next/examples/todo-list
> node ./dist/migrate

Migrating schemas (alter existing schema)
Connection fails: Error: connect ECONNREFUSED 127.0.0.1:3306
It will be retried for the next request.
events.js:173
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14)
    --------------------
    at Protocol._enqueue (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Connection.js:119:18)
    at Pool.getConnection (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Pool.js:48:16)
    at MySQL.connect (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/loopback-connector-mysql/lib/mysql.js:90:17)
    at Object.initializeDataSource [as initialize] (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/loopback-connector-mysql/lib/mysql.js:44:28)
    at DbDataSource.DataSource.setup (/Users/bajtos/src/loopback/next/packages/repository/node_modules/loopback-datasource-juggler/lib/datasource.js:499:19)
    at new DataSource (/Users/bajtos/src/loopback/next/packages/repository/node_modules/loopback-datasource-juggler/lib/datasource.js:144:8)
    at new DbDataSource (/Users/bajtos/src/loopback/next/examples/todo-list/dist/datasources/db.datasource.js:24:9)
    at /Users/bajtos/src/loopback/next/packages/context/dist/resolver.js:47:16
    at Object.transformValueOrPromise (/Users/bajtos/src/loopback/next/packages/context/dist/value-promise.js:227:16)
    at Object.instantiateClass (/Users/bajtos/src/loopback/next/packages/context/dist/resolver.js:42:34)
    at /Users/bajtos/src/loopback/next/packages/context/dist/binding.js:407:46
    at Binding._getValue (/Users/bajtos/src/loopback/next/packages/context/dist/binding.js:295:20)
    at /Users/bajtos/src/loopback/next/packages/context/dist/binding.js:201:29
    at /Users/bajtos/src/loopback/next/packages/context/dist/resolution-session.js:69:53
    at Object.tryWithFinally (/Users/bajtos/src/loopback/next/packages/context/dist/value-promise.js:157:18)
    at Function.runWithBinding (/Users/bajtos/src/loopback/next/packages/context/dist/resolution-session.js:69:32)
    at Binding.getValue (/Users/bajtos/src/loopback/next/packages/context/dist/binding.js:199:67)
    at TodoListApplication.getValueOrPromise (/Users/bajtos/src/loopback/next/packages/context/dist/context.js:653:36)
    at /Users/bajtos/src/loopback/next/packages/context/dist/resolver.js:105:24
    at /Users/bajtos/src/loopback/next/packages/context/dist/resolution-session.js:89:53
Emitted 'error' event at:
    at DbDataSource.postInit (/Users/bajtos/src/loopback/next/packages/repository/node_modules/loopback-datasource-juggler/lib/datasource.js:486:16)
    at /Users/bajtos/src/loopback/next/examples/todo-list/node_modules/loopback-connector-mysql/lib/mysql.js:102:19
    at Handshake.onConnect (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Pool.js:58:9)
    at Handshake.<anonymous> (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Connection.js:525:10)
    at Handshake._callback (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Connection.js:491:16)
    at Handshake.Sequence.end (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Protocol.handleNetworkError (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/protocol/Protocol.js:369:14)
    at PoolConnection.Connection._handleNetworkError (/Users/bajtos/src/loopback/next/examples/todo-list/node_modules/mysql/lib/Connection.js:421:18)
    at Socket.emit (events.js:196:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:17)

This may be a design problem in juggler, where the DataSource and/or the connectors don't correctly propagate connection errors from the connection pool to the method that triggered initiation of the connection.

@bajtos
Copy link
Member

bajtos commented Jul 2, 2019

Opened a PR to fix this issue - see loopbackio/loopback-datasource-juggler#1756

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug developer-experience Issues affecting ease of use and overall experience of LB users
Projects
None yet
4 participants