From 9b2df5b104b922c3b77ef216e98fb1de1d62504a Mon Sep 17 00:00:00 2001 From: plocket <52798256+plocket@users.noreply.github.com> Date: Thu, 17 Oct 2024 08:13:31 -0400 Subject: [PATCH] Simplify use of `context` in wrapper functionality as per review Co-authored-by: BryceStevenWilley --- lib/docassemble/docassemble_api_interface.js | 79 ++++++++++---------- lib/docassemble/server_install.js | 10 ++- lib/docassemble/setup.js | 10 ++- lib/docassemble/takedown.js | 10 ++- 4 files changed, 56 insertions(+), 53 deletions(-) diff --git a/lib/docassemble/docassemble_api_interface.js b/lib/docassemble/docassemble_api_interface.js index c6e69dff..03937147 100644 --- a/lib/docassemble/docassemble_api_interface.js +++ b/lib/docassemble/docassemble_api_interface.js @@ -61,9 +61,9 @@ const time = require('../utils/time'); let da_i = {}; module.exports = da_i; -const artifacts_path = session_vars.get_artifacts_path_name(); -const log = new Log({ path: artifacts_path, context: `api` }); const context = `da api`; +const artifacts_path = session_vars.get_artifacts_path_name(); +const log = new Log({ path: artifacts_path, context }); // Constants @@ -96,7 +96,7 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) { // Defaults let { timeout } = options || { timeout: DEFAULT_MAX_REQUEST_MS }; - log.info({ code: `ALK0166`, context: `da api`, }, + log.info({ code: `ALK0166`, context, }, `Waiting for the docassemble server to respond. Will wait for ${ timeout/1000 } seconds at most.` ); @@ -106,7 +106,7 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) { let response = await _da_REST.get_dev_id( timeout ); let end_time = Date.now() if ( response && response.data && response.data.id ) { - log.info({ code: `ALK0167`, context: `da api`, }, + log.info({ code: `ALK0167`, context, }, `The docassemble server responded after ${ (end_time - start_time)/1000 } seconds.` ); return false; @@ -117,13 +117,13 @@ da_i.throw_an_error_if_server_is_not_responding = async function ( options ) { if ( is_timeout_error( error ) ) { // Anything that's waiting for the server to not be busy does not want // to move forward without a free server. - log.info({ code: `ALK0168`, context: `da api`, }, + log.info({ code: `ALK0168`, context, }, `The docassemble server was still busy after ${ timeout/1000 } seconds.` ); } else { // Not sure what to do for other errors. It could be a server // error, like an 'Access Denied' error, which should be handled. - log.warning({ code: `ALK0169`, context: `da api` }, + log.warning({ code: `ALK0169`, context, }, `Checked if the server was busy and ran into a different error`, error ); @@ -194,7 +194,7 @@ da_i.get_api_key_access_status = async function ({ timeout = null, api_key = nul * to allow for server request. */ - let auth_data = { + let auth_feedback = { codes: [ 1 ], has_auth: null, messages: [`API key authorization is not yet determined.`], @@ -203,16 +203,16 @@ da_i.get_api_key_access_status = async function ({ timeout = null, api_key = nul // Skip this for tests run from ALKilnInThePlayground if ( session_vars.get_origin() === 'playground' ) { - auth_data.codes.push( 2 ); - auth_data.messages.push( `API keys are unnecessary for tests run in the Playground.` ); + auth_feedback.codes.push( 2 ); + auth_feedback.messages.push( `API keys are unnecessary for tests run in the Playground.` ); } if ( typeof api_key !== `string` ) { - auth_data.codes.push( 3 ); - auth_data.messages.push( `The given API key is not a string. Type: ${ typeof api_key }`); + auth_feedback.codes.push( 3 ); + auth_feedback.messages.push( `The given API key is not a string. Type: ${ typeof api_key }`); } - if ( auth_data.codes.length === 1 ) { + if ( auth_feedback.codes.length === 1 ) { // Default timeouts timeout = timeout || DEFAULT_MAX_REQUEST_MS; try { @@ -220,33 +220,31 @@ da_i.get_api_key_access_status = async function ({ timeout = null, api_key = nul api_key, server_url: session_vars.get_da_server_url(), }); - auth_data.has_auth = true; - auth_data.codes = [ 6 ]; - auth_data.messages = [ `This API key has at least basic authorization on this server.` ]; + auth_feedback.has_auth = true; + auth_feedback.codes = [ 6 ]; + auth_feedback.messages = [ `This API key has at least basic authorization on this server.` ]; } catch ( error ) { - auth_data.error = error; + auth_feedback.error = error; if ( error.status === 403 ) { - auth_data.has_auth = false; - auth_data.codes = [ 4 ]; - auth_data.messages = [ `This API is unverified on this server. It is probably missing or has been deleted.` ]; + auth_feedback.has_auth = false; + auth_feedback.codes = [ 4 ]; + auth_feedback.messages = [ `This API is unverified on this server. It is probably missing or has been deleted.` ]; } else { // Note on 404, non-existent key: 404 == 403 - Since we're using the // key itself to get its own info, if it doesn't exist, we'll get 403 - auth_data.codes = [ 5 ]; - auth_data.messages = [ `This API key threw a non-403 error.` ]; + auth_feedback.codes = [ 5 ]; + auth_feedback.messages = [ `This API key threw a non-403 error.` ]; } // ends error status } // ends try - } // ends auth_data.codes only has the first code + } // ends auth_feedback.codes only has the first code - // log.debug({ type: `da api info`, code: `ALKx0202`, - // pre: `API key authentication info`, data: auth_data }); - log.debug({ code: `ALK0202`, context: `da api`, }, - `API key authentication info`, - auth_data + log.debug({ code: `ALK0202`, context, }, + `API key - what happened during authentication:`, + auth_feedback ); - return auth_data; + return auth_feedback; }; // Ends da_i.get_api_key_access_status() // node -e 'require("./lib/docassemble/docassemble_api_interface.js").delete_project( "create1" )' @@ -258,20 +256,19 @@ da_i.delete_project = async function ( delete_options ) { let { timeout } = delete_options || { timeout: DEFAULT_MAX_REQUEST_MS }; let project_name = session_vars.get_project_name() - log.info({ code: `ALK0170`, context: `da api`, }, + log.info({ code: `ALK0170`, context, }, `Trying to delete Project ${ project_name }` ); try { let response = await _da_REST.delete_project( timeout ); - log.info({ code: `ALK0171`, context: `da api`, }, + log.info({ code: `ALK0171`, context, }, `Deleted Project "${ project_name }"` ); } catch ( error ) { - log.throw({ code: `ALK0172`, context: `da api`, error: error }, + log.throw({ code: `ALK0172`, context, error: error }, `Ran into an error when deleting Project "${ project_name }". See https://docassemble.org/docs/api.html#playground_delete_project.` ); - // throw error; } }; // Ends da_i.delete_project() @@ -295,7 +292,7 @@ da_i.quietly_try_to_delete_interviews = async function ({ interviews = {}, api_k }); responses.push( sub_responses ); } else { - log.debug({ code: `ALK0191`, context: `da api` }, + log.debug({ code: `ALK0191`, context, }, `There is no interview to delete.` ); } @@ -312,7 +309,7 @@ da_i.quietly_delete_one_interview = async function ( timeout, { interview = {}, let responses = []; for ( let api_key of api_keys ) { try { - log.debug({ code: `ALK0188`, context: `da api` }, + log.debug({ code: `ALK0188`, context, }, `Trying to delete the interview with the id ${ interview.uid } and an API key.` ); @@ -323,12 +320,12 @@ da_i.quietly_delete_one_interview = async function ( timeout, { interview = {}, }); responses.push( response ); - log.debug({ code: `ALK0189`, context: `da api` }, + log.debug({ code: `ALK0189`, context, }, `No error when trying to delete interview ${ interview.uid } at ${ interview.i }.` ); } catch ( error ) { error.interview = interview; - log.debug({ code: `ALK0190`, context: `da api` }, + log.debug({ code: `ALK0190`, context, }, `Error when deleting the interview. See https://docassemble.org/docs/api.html#session_delete.`, error ); @@ -366,7 +363,7 @@ da_i.get_interview = async function ({ interview = {}, api_keys = [], options = if ( interview && interview.uid ) { for ( let api_key of api_keys ) { try { - log.debug({ code: `ALK0192`, context: `da api` }, + log.debug({ code: `ALK0192`, context, }, `Trying to get the interview with the id ${ interview.uid } and an API key.` ); @@ -377,26 +374,26 @@ da_i.get_interview = async function ({ interview = {}, api_keys = [], options = }); if ( response && response.data && response.data.items && response.data.items[0] && response.data.items[0].session ) { - log.debug({ code: `ALK0193`, context: `da api` }, + log.debug({ code: `ALK0193`, context, }, `Status ${ response.status }. Got ${ response.data.items[0].session }.` ); return response.data.items[0]; } else { - log.debug({ code: `ALK0194`, context: `da api` }, + log.debug({ code: `ALK0194`, context, }, `Status ${ response.status }. Cannot find ${ interview.uid }` ); } } catch ( error ) { error.interview = interview; - log.debug({ code: `ALK0195`, context: `da api` }, + log.debug({ code: `ALK0195`, context, }, `${ error.status || `No error.status.` } ${ error.statusText || error.data || `No error.statusText.` } Ran into an error when getting the interview ${ interview.uid }. See https://docassemble.org/docs/api.html#interviews.`, error ); } } // ends for api_keys } else { - log.debug({ code: `ALK0196`, context: `da api` }, + log.debug({ code: `ALK0196`, context, }, `There is no interview to get.` ); } diff --git a/lib/docassemble/server_install.js b/lib/docassemble/server_install.js index 13fe9e7e..8c9d45a7 100755 --- a/lib/docassemble/server_install.js +++ b/lib/docassemble/server_install.js @@ -4,6 +4,8 @@ const session_vars = require('../utils/session_vars'); const Log = require('../utils/log'); const fg = require('fast-glob'); +const context = `server_install`; + const server_install = async () => { /** Finds out where in this directory the package's folders are. */ session_vars.save_install_method(`server`); @@ -25,17 +27,17 @@ const server_install = async () => { let chosen_folder = folders[0]; if ( folders.length === 0 ) { let msg = `ALKiln cannot find the package's files. There are 0 folders at "${ path }". Stopping tests.`; - log.throw({ code: `ALK0044`, context: `server_install`, + log.throw({ code: `ALK0044`, context, error: new ReferenceError( msg ) // no logs }); } if ( folders.length > 1 ) { - log.warn({ code: `ALK0045`, context: `setup`, }, + log.warn({ code: `ALK0045`, context, }, `There are ${ folders.length } folders at "${ path }": "${ folders.join('", "') }". ALKiln chose to use the folder "${ chosen_folder }".` ); } else { - log.info({ code: `ALK0218`, context: `setup`, }, + log.info({ code: `ALK0218`, context, }, `ALKiln found the package folder at "${ chosen_folder }".` ); } @@ -50,7 +52,7 @@ session_vars.validateEnvironment(); // Local devs: This will be separate from your test paths // unless you pass a `path` argument into the cucumber run command const argv = require(`minimist`)(process.argv.slice(2)); -const log = new Log({ path: argv.path, context: `server_install` }); +const log = new Log({ path: argv.path, context, }); // 3. run server_install(); diff --git a/lib/docassemble/setup.js b/lib/docassemble/setup.js index 67934729..f127a071 100755 --- a/lib/docassemble/setup.js +++ b/lib/docassemble/setup.js @@ -7,6 +7,8 @@ const session_vars = require(`../utils/session_vars` ); const files = require(`../utils/files` ); const time = require(`../utils/time` ); +const context = `setup`; + const setup = async () => { /** In the docassemble testing account identified by the * DOCASSEMBLE_DEVELOPER_API_KEY secret, create a new Project, @@ -21,7 +23,7 @@ const setup = async () => { const starting_msg = `Starting to upload this package to ` + `a new Project called ${project_name} in the Playground ` + `of your server's testing account. It may take a couple of minutes.`; - log.info({ code: `ALK0046`, context: `setup` }, + log.info({ code: `ALK0046`, context, }, starting_msg ); @@ -50,14 +52,14 @@ const setup = async () => { if ( result.status !== 0 ) { const install_err_msg = `Status code ${ result.status }. Could not ` + `dainstall this package on your server. See above for more details.`; - log.throw({ code: `ALK0047`, context: `setup`, + log.throw({ code: `ALK0047`, context, before: `☝️ ☝️ ☝️ Error data ☝️ ☝️ ☝️\n`, error: `Error while installing project` }, install_err_msg ); } - log.success({ code: `ALK0048`, context: `setup`, }, + log.success({ code: `ALK0048`, context, }, `ALKiln created a Project in your Playground named "${ project_name }"!` ); }; // Ends setup(); @@ -69,7 +71,7 @@ session_vars.validateEnvironment(); // Local devs: This will be separate from your test paths // unless you pass a `path` argument into the cucumber run command const argv = require(`minimist`)(process.argv.slice(2)); -const log = new Log({ path: argv.path, context: `setup` }); +const log = new Log({ path: argv.path, context, }); // 3. run setup(); diff --git a/lib/docassemble/takedown.js b/lib/docassemble/takedown.js index 02256363..eba1659f 100755 --- a/lib/docassemble/takedown.js +++ b/lib/docassemble/takedown.js @@ -4,12 +4,14 @@ const da_i = require('./docassemble_api_interface'); const session_vars = require('../utils/session_vars'); const Log = require('../utils/log'); +const context = `takedown`; + const takedown = async () => { /** In the docassemble testing account identified by the * DOCASSEMBLE_DEVELOPER_API_KEY secret, delete the * Project created for this test. * For local development, clean up project name file. */ - log.info({ code: `ALK0177`, context: `takedown`, }, + log.info({ code: `ALK0177`, context, }, `Trying to remove the docassemble Project from the testing account.` ); // Explicit try-catch prevents "Unhandled promise rejection error" @@ -17,11 +19,11 @@ const takedown = async () => { try { await da_i.delete_project(); session_vars.delete_project_name(); - log.info({ code: `ALK0178`, context: `takedown`, }, + log.info({ code: `ALK0178`, context, }, `Test takedown finished successfully!` ); } catch (error) { - log.throw({ code: `ALK0179`, context: `takedown`, error: error }, + log.throw({ code: `ALK0179`, context, error: error }, `Unable to delete project`, ); // we don't re-throw the exception, so make sure to exit @@ -40,7 +42,7 @@ session_vars.validateEnvironment(); // Local devs: This will be separate from your test artifacts paths // unless you pass a `path` argument into the `cucumber` run command const argv = require(`minimist`)( process.argv.slice(2) ); -const log = new Log({ path: argv.path, context: `takedown` }); +const log = new Log({ path: argv.path, context, }); // 3. run takedown();