From 13136f4b9f1eeb48ea3dc9869552af7202e555a2 Mon Sep 17 00:00:00 2001 From: Intizar Date: Sat, 25 May 2024 14:05:38 +0900 Subject: [PATCH] reflect coderabbitai feedback: refactor --- .../get-error-from-encoding.js | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/contracts/v0.1/scripts/sol-error-mappings/get-error-from-encoding.js b/contracts/v0.1/scripts/sol-error-mappings/get-error-from-encoding.js index 2602d9b3b..47e909b17 100644 --- a/contracts/v0.1/scripts/sol-error-mappings/get-error-from-encoding.js +++ b/contracts/v0.1/scripts/sol-error-mappings/get-error-from-encoding.js @@ -1,13 +1,24 @@ -const fs = require('fs') +const fs = require('node:fs') // Get the key from the terminal argument const hash = process.argv[2] const MAPPING_PATH = 'scripts/sol-error-mappings/errorMappings.json' +function handleLogging({ exitCode, logMessage, errorMessage }) { + if (logMessage) { + console.log(logMessage) + } + if (errorMessage) { + console.error(errorMessage) + } + if (exitCode) { + process.exit(exitCode) + } +} + function main() { if (!hash) { - console.error('hash not provided') - process.exit(1) + handleLogging({ exitCode: 1, errorMessage: 'hash not provided' }) } try { @@ -15,13 +26,12 @@ function main() { const obj = JSON.parse(data) const value = obj[hash] if (!value) { - console.error('----hash not found----') + handleLogging({ exitCode: 1, errorMessage: 'hash not found' }) } else { - console.log(value) + handleLogging({ logMessage: value }) } } catch (error) { - console.error(error) - process.exit(1) + handleLogging({ exitCode: 1, errorMessage: error.message }) } }