UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

97 lines (88 loc) 4.1 kB
/** * File manager error codes */ export const FILE_MANAGER_ERROR_CODES = { NOT_FOUND: 'NOT_FOUND', INVALID_PATH: 'INVALID_PATH', PERMISSION_DENIED: 'PERMISSION_DENIED', ACCESS_DENIED: 'ACCESS_DENIED', ALREADY_EXISTS: 'ALREADY_EXISTS', NOT_A_FILE: 'NOT_A_FILE', NOT_A_DIRECTORY: 'NOT_A_DIRECTORY', DIRECTORY_NOT_EMPTY: 'DIRECTORY_NOT_EMPTY', OUTSIDE_ROOT: 'OUTSIDE_ROOT', IO_ERROR: 'IO_ERROR', LOCK_ERROR: 'LOCK_ERROR', INVALID_OPERATION: 'INVALID_OPERATION', EXTENDED_ATTRIBUTE_NOT_FOUND: 'EXTENDED_ATTRIBUTE_NOT_FOUND', TAG_NOT_FOUND: 'TAG_NOT_FOUND', FILE_NOT_FOUND: 'FILE_NOT_FOUND', PARENT_DIRECTORY_NOT_FOUND: 'PARENT_DIRECTORY_NOT_FOUND', UNKNOWN: 'UNKNOWN', } as const; /** * File manager error messages */ export const FILE_MANAGER_ERROR_MESSAGES = { [FILE_MANAGER_ERROR_CODES.NOT_FOUND]: 'File or directory not found: {path}', [FILE_MANAGER_ERROR_CODES.INVALID_PATH]: 'Invalid path: {path}', [FILE_MANAGER_ERROR_CODES.PERMISSION_DENIED]: 'Permission denied: {path}', [FILE_MANAGER_ERROR_CODES.ACCESS_DENIED]: 'Access denied: {path}', [FILE_MANAGER_ERROR_CODES.ALREADY_EXISTS]: 'File or directory already exists: {path}', [FILE_MANAGER_ERROR_CODES.NOT_A_FILE]: 'Path is not a file: {path}', [FILE_MANAGER_ERROR_CODES.NOT_A_DIRECTORY]: 'Path is not a directory: {path}', [FILE_MANAGER_ERROR_CODES.DIRECTORY_NOT_EMPTY]: 'Directory not empty: {path}', [FILE_MANAGER_ERROR_CODES.OUTSIDE_ROOT]: 'Path is outside the root directory', [FILE_MANAGER_ERROR_CODES.IO_ERROR]: 'IO error: {message}', [FILE_MANAGER_ERROR_CODES.LOCK_ERROR]: 'Lock error: {message}', [FILE_MANAGER_ERROR_CODES.INVALID_OPERATION]: 'Invalid operation: {message}', [FILE_MANAGER_ERROR_CODES.EXTENDED_ATTRIBUTE_NOT_FOUND]: 'Extended attribute not found', [FILE_MANAGER_ERROR_CODES.TAG_NOT_FOUND]: 'Tag not found', [FILE_MANAGER_ERROR_CODES.FILE_NOT_FOUND]: 'File not found', [FILE_MANAGER_ERROR_CODES.PARENT_DIRECTORY_NOT_FOUND]: 'Parent directory does not exist', [FILE_MANAGER_ERROR_CODES.UNKNOWN]: 'Unknown error', } as const; /** * File manager error class */ export class FileManagerError extends Error { constructor( message: string, public code: keyof typeof FILE_MANAGER_ERROR_CODES, public path?: string, public originalError?: Error, ) { // Format the message by replacing placeholders let formattedMessage = message; // If the message contains {message} placeholder and we have an original error if (message.includes('{message}') && originalError) { formattedMessage = formattedMessage.replace('{message}', originalError.message); } else if (message.includes('{message}')) { // If we don't have an original error but have {message} placeholder // Remove the placeholder without replacement formattedMessage = formattedMessage.replace(': {message}', ''); } // Replace path placeholder if exists if (path) { formattedMessage = formattedMessage.replace('{path}', path); } // Create the full error message with path if available const fullMessage = path ? `${formattedMessage} (Path: ${path})` : formattedMessage; super(fullMessage); this.name = 'FileManagerError'; } toString(): string { return `${this.name}: ${this.message}`; } } // For backwards compatibility, export the old error message constant export const ERROR_MESSAGES = { FILE_NOT_FOUND: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.FILE_NOT_FOUND], DIRECTORY_NOT_FOUND: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.NOT_FOUND], OUTSIDE_ROOT: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.OUTSIDE_ROOT], INVALID_PATH: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.INVALID_PATH], NOT_A_FILE: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.NOT_A_FILE], NOT_A_DIRECTORY: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.NOT_A_DIRECTORY], ALREADY_EXISTS: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.ALREADY_EXISTS], ACCESS_DENIED: FILE_MANAGER_ERROR_MESSAGES[FILE_MANAGER_ERROR_CODES.ACCESS_DENIED], };