first commit
This commit is contained in:
commit
7988853b57
43 changed files with 8415 additions and 0 deletions
42
src/utils/error_handler.js
Normal file
42
src/utils/error_handler.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* error_handler.js
|
||||
*
|
||||
* Provides standardized error handling for Slack responses
|
||||
*/
|
||||
const logger = require('./logger');
|
||||
|
||||
/**
|
||||
* Handle errors consistently across handlers
|
||||
*
|
||||
* @param {Error} error - The error that occurred
|
||||
* @param {string} source - Source file/function where error occurred
|
||||
* @param {Function} respond - Slack respond function
|
||||
* @param {Object} options - Additional options
|
||||
* @param {boolean} options.replaceOriginal - Whether to replace original message
|
||||
* @param {string} options.responseType - Response type (ephemeral/in_channel)
|
||||
* @param {string} options.customMessage - Custom message to display instead of error
|
||||
*/
|
||||
const handleError = async (error, source, respond, options = {}) => {
|
||||
const {
|
||||
replaceOriginal = false,
|
||||
responseType = 'ephemeral',
|
||||
customMessage = null
|
||||
} = options;
|
||||
|
||||
// Log the error with consistent format
|
||||
logger.error(`${source}: ${error.message}`);
|
||||
logger.debug(`${source}: Error stack: ${error.stack}`);
|
||||
|
||||
// Respond to user with appropriate message
|
||||
const displayMessage = customMessage || `An unexpected error occurred: ${error.message}`;
|
||||
|
||||
await respond({
|
||||
text: displayMessage,
|
||||
replace_original: replaceOriginal,
|
||||
response_type: responseType
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
handleError
|
||||
};
|
10
src/utils/file_utils.js
Normal file
10
src/utils/file_utils.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
// file_utils.js
|
||||
const path = require('path');
|
||||
|
||||
function getFileName(filePath) {
|
||||
return path.basename(filePath);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFileName
|
||||
};
|
0
src/utils/formatters.js
Normal file
0
src/utils/formatters.js
Normal file
103
src/utils/logger.js
Normal file
103
src/utils/logger.js
Normal file
|
@ -0,0 +1,103 @@
|
|||
/**
|
||||
* logger.js
|
||||
* Handles logging functionality
|
||||
*/
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { LOGGING_CONFIG } = require('../config/appConfig');
|
||||
|
||||
// Define log levels and their priority (higher number = higher priority)
|
||||
const LOG_LEVELS = {
|
||||
DEBUG: 1,
|
||||
INFO: 2,
|
||||
WARN: 3,
|
||||
ERROR: 4
|
||||
};
|
||||
|
||||
// Get configured log level from config, default to INFO if not specified
|
||||
const configuredLevel = (LOGGING_CONFIG?.level || 'info').toUpperCase();
|
||||
const configuredLevelValue = LOG_LEVELS[configuredLevel] || LOG_LEVELS.INFO;
|
||||
|
||||
// Ensure logs directory exists
|
||||
const LOGS_DIR = path.join(__dirname, '..', '..', 'logs');
|
||||
if (!fs.existsSync(LOGS_DIR)) {
|
||||
fs.mkdirSync(LOGS_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
// Use log file from config if available, otherwise use default
|
||||
const LOG_FILE = LOGGING_CONFIG?.file
|
||||
? path.resolve(path.join(__dirname, '..', '..'), LOGGING_CONFIG.file)
|
||||
: path.join(LOGS_DIR, 'fylgja.log');
|
||||
|
||||
// Create logger object
|
||||
const logger = {
|
||||
/**
|
||||
* Internal method to write log entry to file and console if level meets threshold
|
||||
* @param {string} level - Log level (DEBUG, INFO, WARN, ERROR)
|
||||
* @param {string} message - Log message to write
|
||||
*/
|
||||
_writeToFile: (level, message) => {
|
||||
// Check if this log level should be displayed based on configured level
|
||||
const levelValue = LOG_LEVELS[level] || 0;
|
||||
|
||||
if (levelValue >= configuredLevelValue) {
|
||||
const timestamp = new Date().toISOString();
|
||||
const logEntry = `${timestamp} ${level}: ${message}\n`;
|
||||
|
||||
// Append to log file
|
||||
try {
|
||||
fs.appendFileSync(LOG_FILE, logEntry);
|
||||
} catch (err) {
|
||||
console.error(`Failed to write to log file: ${err.message}`);
|
||||
}
|
||||
|
||||
// Also log to console with appropriate method
|
||||
switch (level) {
|
||||
case 'ERROR':
|
||||
console.error(logEntry.trim());
|
||||
break;
|
||||
case 'WARN':
|
||||
console.warn(logEntry.trim());
|
||||
break;
|
||||
case 'DEBUG':
|
||||
console.debug(logEntry.trim());
|
||||
break;
|
||||
case 'INFO':
|
||||
default:
|
||||
console.info(logEntry.trim());
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Log an info message
|
||||
* @param {string} message - Message to log
|
||||
*/
|
||||
info: (message) => logger._writeToFile('INFO', message),
|
||||
|
||||
/**
|
||||
* Log an error message
|
||||
* @param {string} message - Message to log
|
||||
*/
|
||||
error: (message) => logger._writeToFile('ERROR', message),
|
||||
|
||||
/**
|
||||
* Log a warning message
|
||||
* @param {string} message - Message to log
|
||||
*/
|
||||
warn: (message) => logger._writeToFile('WARN', message),
|
||||
|
||||
/**
|
||||
* Log a debug message
|
||||
* @param {string} message - Message to log
|
||||
*/
|
||||
debug: (message) => logger._writeToFile('DEBUG', message),
|
||||
|
||||
/**
|
||||
* Get the current log level
|
||||
* @returns {string} Current log level
|
||||
*/
|
||||
getLogLevel: () => configuredLevel
|
||||
};
|
||||
|
||||
module.exports = logger;
|
0
src/utils/validators.js
Normal file
0
src/utils/validators.js
Normal file
Loading…
Add table
Add a link
Reference in a new issue