Skip to content

Commit

Permalink
Add error handling for /session request
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Oct 8, 2024
1 parent 3f80add commit c4a4352
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function Strategy(options, verify) {
this._verify = verify
this._url = options.url
this._passReqToCallback = options.passReqToCallback
// Add logging via a "logger" object with logging methods
this._logger = options.logger || {
log: () => {},
warn: () => {},
error: () => {},
}
}

/**
Expand All @@ -60,7 +66,9 @@ Strategy.prototype.authenticate = async function(req, options) {
options = options || {}
const url = options.url || this._url
if (!url) {
return this.fail({ message: "Missing API URL" }, 400)
const message = "EasyDB error: Missing API URL"
this._logger.error(message)
return this.fail({ message }, 400)
}

let username = (req.body && req.body.username) || req.query.username
Expand All @@ -70,9 +78,19 @@ Strategy.prototype.authenticate = async function(req, options) {
}

// Step 1: Start new session
const session = await axios.get(`${url}session`)
let session
try {
session = await axios.get(`${url}session`)
} catch (error) {
session = {
status: 503,
statusText: error.message,
}
}
if (session.status != 200) {
return this.fail({ message: "Error on /session request: " + session.statusText }, session.status)
const message = `EasyDB error on /session request: ${session.statusText}`
this._logger.error(message)
return this.fail({ message }, session.status)
}
// Result: Session token
const token = session.data.token
Expand Down

0 comments on commit c4a4352

Please sign in to comment.