* Classifies image processing errors for analytics. * * Uses error codes when available (Node.js module errors), falls back to * message matching for libraries like sharp that don't expose error codes.
(error: unknown)
| 48 | * message matching for libraries like sharp that don't expose error codes. |
| 49 | */ |
| 50 | function classifyImageError(error: unknown): number { |
| 51 | // Check for Node.js error codes first (more reliable than string matching) |
| 52 | if (error instanceof Error) { |
| 53 | const errorWithCode = error as Error & { code?: string } |
| 54 | if ( |
| 55 | errorWithCode.code === 'MODULE_NOT_FOUND' || |
| 56 | errorWithCode.code === 'ERR_MODULE_NOT_FOUND' || |
| 57 | errorWithCode.code === 'ERR_DLOPEN_FAILED' |
| 58 | ) { |
| 59 | return ERROR_TYPE_MODULE_LOAD |
| 60 | } |
| 61 | if (errorWithCode.code === 'EACCES' || errorWithCode.code === 'EPERM') { |
| 62 | return ERROR_TYPE_PERMISSION |
| 63 | } |
| 64 | if (errorWithCode.code === 'ENOMEM') { |
| 65 | return ERROR_TYPE_MEMORY |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Fall back to message matching for errors without codes |
| 70 | // Note: sharp doesn't expose error codes, so we must match on messages |
| 71 | const message = errorMessage(error) |
| 72 | |
| 73 | // Module loading errors from our native wrapper |
| 74 | if (message.includes('Native image processor module not available')) { |
| 75 | return ERROR_TYPE_MODULE_LOAD |
| 76 | } |
| 77 | |
| 78 | // Sharp/vips processing errors (format detection, corrupt data, etc.) |
| 79 | if ( |
| 80 | message.includes('unsupported image format') || |
| 81 | message.includes('Input buffer') || |
| 82 | message.includes('Input file is missing') || |
| 83 | message.includes('Input file has corrupt header') || |
| 84 | message.includes('corrupt header') || |
| 85 | message.includes('corrupt image') || |
| 86 | message.includes('premature end') || |
| 87 | message.includes('zlib: data error') || |
| 88 | message.includes('zero width') || |
| 89 | message.includes('zero height') |
| 90 | ) { |
| 91 | return ERROR_TYPE_PROCESSING |
| 92 | } |
| 93 | |
| 94 | // Pixel/dimension limit errors from sharp/vips |
| 95 | if ( |
| 96 | message.includes('pixel limit') || |
| 97 | message.includes('too many pixels') || |
| 98 | message.includes('exceeds pixel') || |
| 99 | message.includes('image dimensions') |
| 100 | ) { |
| 101 | return ERROR_TYPE_PIXEL_LIMIT |
| 102 | } |
| 103 | |
| 104 | // Memory allocation failures |
| 105 | if ( |
| 106 | message.includes('out of memory') || |
| 107 | message.includes('Cannot allocate') || |
no test coverage detected