scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
97 lines (88 loc) • 4.1 kB
text/typescript
/**
* 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 or directory not found: {path}',
[]: 'Invalid path: {path}',
[]: 'Permission denied: {path}',
[]: 'Access denied: {path}',
[]: 'File or directory already exists: {path}',
[]: 'Path is not a file: {path}',
[]: 'Path is not a directory: {path}',
[]: 'Directory not empty: {path}',
[]: 'Path is outside the root directory',
[]: 'IO error: {message}',
[]: 'Lock error: {message}',
[]: 'Invalid operation: {message}',
[]: 'Extended attribute not found',
[]: 'Tag not found',
[]: 'File not found',
[]: 'Parent directory does not exist',
[]: '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],
};