Refactor docker parser functions

This commit is contained in:
2026-01-18 00:29:46 +00:00
parent 9cb7297f5b
commit 14836fb989
6 changed files with 455 additions and 463 deletions

View File

@@ -0,0 +1,34 @@
export function detectErrorType(errorLine: string, fullLog: string): string {
const lowerError = errorLine.toLowerCase()
const lowerLog = fullLog.toLowerCase()
if (lowerError.includes('cannot find module') || lowerError.includes('module_not_found')) {
return 'Missing Dependency'
}
if (lowerError.includes('enoent') || lowerError.includes('no such file')) {
return 'File Not Found'
}
if (lowerLog.includes('arm64') || lowerLog.includes('amd64') || lowerError.includes('platform')) {
return 'Platform/Architecture Issue'
}
if (lowerError.includes('permission denied') || lowerError.includes('eacces')) {
return 'Permission Error'
}
if (lowerError.includes('network') || lowerError.includes('timeout') || lowerError.includes('connection')) {
return 'Network Error'
}
if (lowerError.includes('syntax') || lowerError.includes('unexpected')) {
return 'Syntax Error'
}
if (lowerError.includes('memory') || lowerError.includes('out of')) {
return 'Resource Limit'
}
return 'Build Failure'
}

View File

@@ -0,0 +1,147 @@
import { DockerError, Solution } from '@/types/docker'
export function getSolutionsForError(error: DockerError): Solution[] {
const solutions: Solution[] = []
const type = error.type.toLowerCase()
if (type.includes('missing dependency') || type.includes('module')) {
if (error.message.includes('@rollup/rollup') || error.message.includes('rollup')) {
solutions.push({
title: 'Install Platform-Specific Rollup Dependencies',
description:
'The Rollup bundler requires platform-specific native binaries. For multi-platform Docker builds, you need to ensure optional dependencies are installed.',
steps: [
'Update your Dockerfile to force install optional dependencies',
'Use --platform flag to ensure correct architecture binaries',
'Consider using --legacy-peer-deps flag'
],
code: `# In your Dockerfile, change the npm install line to:
RUN npm install --legacy-peer-deps --include=optional
# Or explicitly install the missing package:
RUN npm install @rollup/rollup-linux-arm64-musl --save-optional`,
codeLanguage: 'dockerfile'
})
solutions.push({
title: 'Update package.json optionalDependencies',
description: 'Explicitly declare platform-specific Rollup dependencies as optional.',
steps: [
'Add optionalDependencies section to package.json',
'Include all platform variants of Rollup',
'Rebuild your Docker image'
],
code: `{
"optionalDependencies": {
"@rollup/rollup-linux-arm64-musl": "^4.53.3",
"@rollup/rollup-linux-x64-musl": "^4.53.3",
"@rollup/rollup-darwin-arm64": "^4.53.3",
"@rollup/rollup-darwin-x64": "^4.53.3"
}
}`,
codeLanguage: 'json'
})
} else {
solutions.push({
title: 'Install Missing Node Module',
description: 'A required npm package is not installed in your Docker image.',
steps: [
'Verify the package is listed in package.json',
'Ensure npm install runs before the build step',
'Check for typos in import statements'
],
code: `# Make sure your Dockerfile includes:
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build`,
codeLanguage: 'dockerfile'
})
}
}
if (type.includes('platform') || type.includes('architecture')) {
solutions.push({
title: 'Build for Single Platform',
description:
"If you don't need multi-platform support, build for a single architecture to avoid complexity.",
steps: [
'Remove --platform flag from docker build command',
'Or specify only one platform: --platform linux/amd64',
'This will speed up builds and avoid architecture-specific issues'
],
code: `# Instead of:
docker buildx build --platform linux/amd64,linux/arm64 .
# Use:
docker buildx build --platform linux/amd64 .`,
codeLanguage: 'bash'
})
solutions.push({
title: 'Use QEMU for Cross-Platform Builds',
description: 'Set up proper emulation for building ARM images on x64 hosts.',
steps: [
'Install QEMU binfmt support',
'Create a new buildx builder instance',
'Verify the builder supports multiple platforms'
],
code: `# Set up buildx with QEMU
docker run --privileged --rm tonistiigi/binfmt --install all
docker buildx create --name multiplatform --driver docker-container --use
docker buildx inspect --bootstrap`,
codeLanguage: 'bash'
})
}
if (type.includes('file not found')) {
solutions.push({
title: 'Check File Paths and .dockerignore',
description: 'Verify that all required files are copied into the Docker build context.',
steps: [
"Check .dockerignore to ensure needed files aren't excluded",
'Verify COPY commands use correct paths',
'Ensure files exist in your repository'
],
code: `# In .dockerignore, make sure you're not ignoring needed files
# Remove these if they're blocking required files:
# node_modules
# dist
# build`,
codeLanguage: 'text'
})
}
if (type.includes('permission')) {
solutions.push({
title: 'Fix File Permissions',
description: "The Docker build process doesn't have permission to access required files.",
steps: [
'Check file permissions in your repository',
'Add RUN chmod commands if needed',
'Consider using a non-root user correctly'
],
code: `# In Dockerfile, add permission fixes:
RUN chmod +x /app/scripts/*.sh
RUN chown -R node:node /app`,
codeLanguage: 'dockerfile'
})
}
if (solutions.length === 0) {
solutions.push({
title: 'General Docker Build Troubleshooting',
description: 'Try these common fixes for Docker build issues.',
steps: [
'Clear Docker build cache: docker builder prune',
'Rebuild without cache: docker build --no-cache',
'Check Docker daemon logs for more details',
'Verify your Dockerfile syntax is correct',
'Ensure base images are accessible and up to date'
]
})
}
return solutions
}

View File

@@ -0,0 +1,4 @@
import { KnowledgeBaseItem } from '@/types/docker'
import knowledgeBaseData from '@/data/docker-knowledge-base.json'
export const knowledgeBase = knowledgeBaseData as KnowledgeBaseItem[]

View File

@@ -0,0 +1,61 @@
import { DockerError } from '@/types/docker'
import { detectErrorType } from './detectErrorType'
export function parseDockerLog(log: string): DockerError[] {
const errors: DockerError[] = []
const lines = log.split('\n')
let currentError: Partial<DockerError> | null = null
let contextLines: string[] = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line.includes('ERROR:') || line.includes('Error:')) {
if (currentError) {
errors.push({
id: Math.random().toString(36).substr(2, 9),
type: currentError.type || 'Unknown Error',
message: currentError.message || 'An error occurred',
stage: currentError.stage,
exitCode: currentError.exitCode,
context: contextLines.slice(-5),
severity: 'critical'
})
}
currentError = {
message: line.replace(/^.*?ERROR:\s*/, '').replace(/^.*?Error:\s*/, '').trim()
}
contextLines = [line]
const stageMatch = log.match(/\[([^\]]+)\s+\d+\/\d+\]/)
if (stageMatch) {
currentError.stage = stageMatch[1]
}
const exitCodeMatch = line.match(/exit code[:\s]+(\d+)/i)
if (exitCodeMatch) {
currentError.exitCode = parseInt(exitCodeMatch[1], 10)
}
currentError.type = detectErrorType(line, log)
} else if (currentError) {
contextLines.push(line)
}
}
if (currentError) {
errors.push({
id: Math.random().toString(36).substr(2, 9),
type: currentError.type || 'Unknown Error',
message: currentError.message || 'An error occurred',
stage: currentError.stage,
exitCode: currentError.exitCode,
context: contextLines.slice(-5),
severity: 'critical'
})
}
return errors
}