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

Check db startup and pass siginin error code #185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions application/api/auth.2/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
access: 'public',
method: async ({ login, password }) => {
const user = await api.auth.provider.getUser(login);
if (!user) throw new Error('Incorrect login or password');
if (!user) throw new Error('Incorrect login or password', 403);
const { accountId, password: hash } = user;
const valid = await metarhia.metautil.validatePassword(password, hash);
if (!valid) throw new Error('Incorrect login or password');
if (!valid) throw new Error('Incorrect login or password', 403);
console.log(`Logged user: ${login}`);
const token = api.auth.provider.generateToken();
const data = { accountId: user.accountId };
Expand Down
8 changes: 8 additions & 0 deletions application/db/pg/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ async () => {
}
const options = { ...config.database, console };
db.pg = new metarhia.metasql.Database(options);

// Check that we connected successfully.
await db.pg.query('SELECT now()').catch((err) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow detect connection status without a query?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To example, we can catch connection errors by manually creating connection using connect method from Pool:

await db.pg.pool.connect().catch((err) => {
....
});

if (application.worker.id === 'W1') {
console.error(`Failed to setup DB: ${err.message}`);
}
process.exit(0);
});
};