From 7488faad55512a220bfbbc87649a401c1fc17c19 Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Mon, 22 Jan 2024 14:25:35 -0800 Subject: [PATCH] add error.code(s) --- lib/main.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/main.js b/lib/main.js index f5b4ffa6..7da182bd 100644 --- a/lib/main.js +++ b/lib/main.js @@ -53,7 +53,9 @@ function _parseVault (options) { // Parse .env.vault const result = DotenvModule.configDotenv({ path: vaultPath }) if (!result.parsed) { - throw new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`) + const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`) + err.code = 'MISSING_DATA' + throw err } // handle scenario for comma separated keys - for use with key rotation @@ -121,7 +123,9 @@ function _instructions (result, dotenvKey) { uri = new URL(dotenvKey) } catch (error) { if (error.code === 'ERR_INVALID_URL') { - throw new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development') + const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development') + err.code = 'INVALID_DOTENV_KEY' + throw err } throw error @@ -130,20 +134,26 @@ function _instructions (result, dotenvKey) { // Get decrypt key const key = uri.password if (!key) { - throw new Error('INVALID_DOTENV_KEY: Missing key part') + const err = new Error('INVALID_DOTENV_KEY: Missing key part') + err.code = 'INVALID_DOTENV_KEY' + throw err } // Get environment const environment = uri.searchParams.get('environment') if (!environment) { - throw new Error('INVALID_DOTENV_KEY: Missing environment part') + const err = new Error('INVALID_DOTENV_KEY: Missing environment part') + err.code = 'INVALID_DOTENV_KEY' + throw err } // Get ciphertext payload const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}` const ciphertext = result.parsed[environmentKey] // DOTENV_VAULT_PRODUCTION if (!ciphertext) { - throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`) + const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`) + err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT' + throw err } return { ciphertext, key } @@ -255,14 +265,14 @@ function decrypt (encrypted, keyStr) { const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data' if (isRange || invalidKeyLength) { - const msg = 'INVALID_DOTENV_KEY: It must be 64 characters long (or more)' - throw new Error(msg) + const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)') + err.code = 'INVALID_DOTENV_KEY' + throw err } else if (decryptionFailed) { - const msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY' - throw new Error(msg) + const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY') + err.code = 'DECRYPTION_FAILED' + throw err } else { - console.error('Error: ', error.code) - console.error('Error: ', error.message) throw error } } @@ -274,7 +284,9 @@ function populate (processEnv, parsed, options = {}) { const override = Boolean(options && options.override) if (typeof parsed !== 'object') { - throw new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate') + const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate') + err.code = 'OBJECT_REQUIRED' + throw err } // Set process.env