|
| 1 | +import type { ApiProblemDetails } from "@/lib/api-problem"; |
| 2 | + |
| 3 | +export type AzureExtractorUploadFailureKind = "schema" | "archive" | "validation" | "unknown"; |
| 4 | + |
| 5 | +export const AZURE_EXTRACTOR_UPLOAD_DOC_PATHS = { |
| 6 | + primary: "/docs/library/AZURE_EXTRACTOR.md", |
| 7 | + ingestRunbook: "/docs/runbooks/AZURE_EXTRACTOR_INGEST.md", |
| 8 | +} as const; |
| 9 | + |
| 10 | +export type AzureExtractorUploadSemanticCode = |
| 11 | + | "AZURE_EXTRACTOR_UNSUPPORTED_SCHEMA_VERSION" |
| 12 | + | "AZURE_EXTRACTOR_MISSING_MANIFEST" |
| 13 | + | "AZURE_EXTRACTOR_INVALID_MANIFEST_JSON" |
| 14 | + | "AZURE_EXTRACTOR_MISSING_SCHEMA_VERSION" |
| 15 | + | "AZURE_EXTRACTOR_MISSING_RESOURCES_JSON" |
| 16 | + | "AZURE_EXTRACTOR_INVALID_ZIP_ARCHIVE" |
| 17 | + | "AZURE_EXTRACTOR_RUN_SCOPE_MISMATCH" |
| 18 | + | "AZURE_EXTRACTOR_ZIP_TOO_LARGE" |
| 19 | + | "AZURE_EXTRACTOR_NO_FILE_UPLOADED" |
| 20 | + | "AZURE_EXTRACTOR_UPLOAD_UNKNOWN"; |
| 21 | + |
| 22 | +export type AzureExtractorUploadErrorResolution = { |
| 23 | + semanticCode: AzureExtractorUploadSemanticCode; |
| 24 | + failureKind: AzureExtractorUploadFailureKind; |
| 25 | + heading: string; |
| 26 | + guidance: string; |
| 27 | + docPath: string; |
| 28 | +}; |
| 29 | + |
| 30 | +function readFailureKind(problem: ApiProblemDetails | null): AzureExtractorUploadFailureKind { |
| 31 | + const raw = problem?.failureKind; |
| 32 | + |
| 33 | + if (raw === "schema" || raw === "archive" || raw === "validation") { |
| 34 | + return raw; |
| 35 | + } |
| 36 | + |
| 37 | + const detail = problem?.detail?.toLowerCase() ?? ""; |
| 38 | + |
| 39 | + if (detail.includes("schemaversion") || detail.includes("manifest.json")) { |
| 40 | + return "schema"; |
| 41 | + } |
| 42 | + |
| 43 | + if (detail.includes("zip") || detail.includes("archive")) { |
| 44 | + return "archive"; |
| 45 | + } |
| 46 | + |
| 47 | + if (detail.includes("run id") || detail.includes("resources.json") || detail.includes("maximum size")) { |
| 48 | + return "validation"; |
| 49 | + } |
| 50 | + |
| 51 | + return "unknown"; |
| 52 | +} |
| 53 | + |
| 54 | +function resolveSemanticCode(detail: string, failureKind: AzureExtractorUploadFailureKind): AzureExtractorUploadSemanticCode { |
| 55 | + const normalized = detail.toLowerCase(); |
| 56 | + |
| 57 | + if (normalized.includes("unsupported manifest schemaversion")) { |
| 58 | + return "AZURE_EXTRACTOR_UNSUPPORTED_SCHEMA_VERSION"; |
| 59 | + } |
| 60 | + |
| 61 | + if (normalized.includes("does not contain manifest.json")) { |
| 62 | + return "AZURE_EXTRACTOR_MISSING_MANIFEST"; |
| 63 | + } |
| 64 | + |
| 65 | + if (normalized.includes("not valid json")) { |
| 66 | + return "AZURE_EXTRACTOR_INVALID_MANIFEST_JSON"; |
| 67 | + } |
| 68 | + |
| 69 | + if (normalized.includes("schemaversion") && normalized.includes("missing")) { |
| 70 | + return "AZURE_EXTRACTOR_MISSING_SCHEMA_VERSION"; |
| 71 | + } |
| 72 | + |
| 73 | + if (normalized.includes("does not contain resources.json")) { |
| 74 | + return "AZURE_EXTRACTOR_MISSING_RESOURCES_JSON"; |
| 75 | + } |
| 76 | + |
| 77 | + if (normalized.includes("not a valid zip archive")) { |
| 78 | + return "AZURE_EXTRACTOR_INVALID_ZIP_ARCHIVE"; |
| 79 | + } |
| 80 | + |
| 81 | + if (normalized.includes("run id is not recognized")) { |
| 82 | + return "AZURE_EXTRACTOR_RUN_SCOPE_MISMATCH"; |
| 83 | + } |
| 84 | + |
| 85 | + if (normalized.includes("exceeds maximum size")) { |
| 86 | + return "AZURE_EXTRACTOR_ZIP_TOO_LARGE"; |
| 87 | + } |
| 88 | + |
| 89 | + if (normalized.includes("no file uploaded")) { |
| 90 | + return "AZURE_EXTRACTOR_NO_FILE_UPLOADED"; |
| 91 | + } |
| 92 | + |
| 93 | + if (failureKind === "schema") { |
| 94 | + return "AZURE_EXTRACTOR_MISSING_SCHEMA_VERSION"; |
| 95 | + } |
| 96 | + |
| 97 | + if (failureKind === "archive") { |
| 98 | + return "AZURE_EXTRACTOR_INVALID_ZIP_ARCHIVE"; |
| 99 | + } |
| 100 | + |
| 101 | + return "AZURE_EXTRACTOR_UPLOAD_UNKNOWN"; |
| 102 | +} |
| 103 | + |
| 104 | +function guidanceForSemanticCode(code: AzureExtractorUploadSemanticCode): string { |
| 105 | + switch (code) { |
| 106 | + case "AZURE_EXTRACTOR_UNSUPPORTED_SCHEMA_VERSION": |
| 107 | + return "Re-run Get-ArchLucidAzurePackage.ps1 from the current CDN script so manifest.json uses a supported schemaVersion, then upload the new ZIP."; |
| 108 | + case "AZURE_EXTRACTOR_MISSING_MANIFEST": |
| 109 | + return "The ZIP must contain manifest.json at the archive root. Re-run the extractor script and upload the complete package."; |
| 110 | + case "AZURE_EXTRACTOR_INVALID_MANIFEST_JSON": |
| 111 | + return "manifest.json is not valid JSON. Re-run the extractor locally and confirm the file opens cleanly before uploading."; |
| 112 | + case "AZURE_EXTRACTOR_MISSING_SCHEMA_VERSION": |
| 113 | + return "manifest.json must include schemaVersion 1. Download the latest extractor script and regenerate the ZIP."; |
| 114 | + case "AZURE_EXTRACTOR_MISSING_RESOURCES_JSON": |
| 115 | + return "The ZIP must include resources.json from Get-ArchLucidAzurePackage.ps1. Do not upload a manifest-only archive."; |
| 116 | + case "AZURE_EXTRACTOR_INVALID_ZIP_ARCHIVE": |
| 117 | + return "Upload a complete .zip produced by Get-ArchLucidAzurePackage.ps1. Partial downloads or renamed folders often fail archive validation."; |
| 118 | + case "AZURE_EXTRACTOR_RUN_SCOPE_MISMATCH": |
| 119 | + return "The runId query parameter does not match a review in this workspace. Upload without runId or open the correct workspace scope first."; |
| 120 | + case "AZURE_EXTRACTOR_ZIP_TOO_LARGE": |
| 121 | + return "Reduce extractor scope (subscription or resource group) or use chunked upload when enabled. Confirm the ZIP is within the server size limit."; |
| 122 | + case "AZURE_EXTRACTOR_NO_FILE_UPLOADED": |
| 123 | + return "Select a .zip file in the upload control. The multipart form field must be named file."; |
| 124 | + default: |
| 125 | + return "Review the error detail, fix the extractor package, and retry. Include the copied error details when opening a support ticket."; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function headingForFailureKind(failureKind: AzureExtractorUploadFailureKind): string { |
| 130 | + if (failureKind === "schema") { |
| 131 | + return "Extractor manifest rejected"; |
| 132 | + } |
| 133 | + |
| 134 | + if (failureKind === "archive") { |
| 135 | + return "Invalid ZIP archive"; |
| 136 | + } |
| 137 | + |
| 138 | + return "Azure extractor upload failed"; |
| 139 | +} |
| 140 | + |
| 141 | +function docPathForSemanticCode(code: AzureExtractorUploadSemanticCode): string { |
| 142 | + if ( |
| 143 | + code === "AZURE_EXTRACTOR_RUN_SCOPE_MISMATCH" || |
| 144 | + code === "AZURE_EXTRACTOR_ZIP_TOO_LARGE" || |
| 145 | + code === "AZURE_EXTRACTOR_NO_FILE_UPLOADED" |
| 146 | + ) { |
| 147 | + return AZURE_EXTRACTOR_UPLOAD_DOC_PATHS.ingestRunbook; |
| 148 | + } |
| 149 | + |
| 150 | + return AZURE_EXTRACTOR_UPLOAD_DOC_PATHS.primary; |
| 151 | +} |
| 152 | + |
| 153 | +/** Maps Problem Details from extractor upload endpoints into semantic codes and remediation copy. */ |
| 154 | +export function resolveAzureExtractorUploadError( |
| 155 | + problem: ApiProblemDetails | null, |
| 156 | + fallbackMessage: string, |
| 157 | +): AzureExtractorUploadErrorResolution { |
| 158 | + const failureKind = readFailureKind(problem); |
| 159 | + const detail = problem?.detail?.trim() ?? fallbackMessage.trim(); |
| 160 | + const semanticCode = resolveSemanticCode(detail, failureKind); |
| 161 | + |
| 162 | + return { |
| 163 | + semanticCode, |
| 164 | + failureKind, |
| 165 | + heading: headingForFailureKind(failureKind), |
| 166 | + guidance: guidanceForSemanticCode(semanticCode), |
| 167 | + docPath: docPathForSemanticCode(semanticCode), |
| 168 | + }; |
| 169 | +} |
0 commit comments