* Maps a failed compile-service response to a JSON error response: 422 with * extracted TeX errors for compilation failures, 502 for anything unexpected.
( upstreamResponse: Response, requestId: string )
| 168 | * extracted TeX errors for compilation failures, 502 for anything unexpected. |
| 169 | */ |
| 170 | async function buildCompileErrorResponse( |
| 171 | upstreamResponse: Response, |
| 172 | requestId: string |
| 173 | ): Promise<NextResponse> { |
| 174 | const errorBody = await readResponseJsonWithLimit(upstreamResponse, { |
| 175 | maxBytes: MAX_ERROR_JSON_BYTES, |
| 176 | label: 'LaTeX compile error response', |
| 177 | }).catch(() => undefined) |
| 178 | |
| 179 | const errorRecord = |
| 180 | typeof errorBody === 'object' && errorBody !== null |
| 181 | ? (errorBody as Record<string, unknown>) |
| 182 | : undefined |
| 183 | const errorCode = |
| 184 | typeof errorRecord?.error === 'string' |
| 185 | ? truncate(errorRecord.error, MAX_ERROR_CODE_CHARS) |
| 186 | : undefined |
| 187 | const compilationErrors = extractCompilationErrors(errorRecord?.log_files) |
| 188 | const details = compilationErrors ? `:\n${compilationErrors}` : '' |
| 189 | |
| 190 | const isCompilationFailure = |
| 191 | upstreamResponse.status >= 400 && |
| 192 | upstreamResponse.status < 500 && |
| 193 | Boolean(errorCode || compilationErrors) |
| 194 | |
| 195 | if (isCompilationFailure) { |
| 196 | logger.warn(`[${requestId}] LaTeX compilation failed`, { |
| 197 | status: upstreamResponse.status, |
| 198 | errorCode, |
| 199 | }) |
| 200 | return NextResponse.json( |
| 201 | { error: `LaTeX compilation failed (${errorCode || upstreamResponse.status})${details}` }, |
| 202 | { status: 422 } |
| 203 | ) |
| 204 | } |
| 205 | |
| 206 | logger.error(`[${requestId}] LaTeX compile service error`, { |
| 207 | status: upstreamResponse.status, |
| 208 | errorCode, |
| 209 | }) |
| 210 | return NextResponse.json( |
| 211 | { error: `LaTeX compile service error: ${upstreamResponse.status}${details}` }, |
| 212 | { status: 502 } |
| 213 | ) |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Stores the compiled PDF as an execution file when execution context is |
no test coverage detected