diff --git a/src/pages/servicos/certidao.vue b/src/pages/servicos/certidao.vue index ac6ff24..c5507a5 100644 --- a/src/pages/servicos/certidao.vue +++ b/src/pages/servicos/certidao.vue @@ -74,6 +74,22 @@ function extrairErro(e) { return e?.data?.description ?? e?.data?.data?.description ?? e?.statusMessage ?? null } +const COD_CERTIDAO_VALIDA_JA_EXISTE = '214' + +// Respostas de erro do endpoint de emissão chegam como ArrayBuffer (responseType +// é 'arrayBuffer' para o PDF). Decodifica o envelope JSON para ler o StandardError. +function lerEnvelopeErro(e) { + let body = e?.data + if (body instanceof ArrayBuffer) { + try { + body = JSON.parse(new TextDecoder().decode(body)) + } catch { + return null + } + } + return body ?? null +} + async function carregarModelos() { if (!docValido.value) { resetModelos() @@ -160,7 +176,8 @@ async function emitir() { return } } catch { - // endpoint indisponível — prossegue com emissão normalmente + // Pré-checagem indisponível — a emissão é bloqueada de forma autoritativa + // pelo backend, que devolve o erro CERTIDAO_VALIDA_JA_EXISTE tratado abaixo. } try { @@ -190,7 +207,14 @@ async function emitir() { etapa.value = 'sucesso' } } catch (e) { - mensagemErro.value = extrairErro(e) ?? 'Erro ao gerar o PDF. Tente novamente.' + const erroBackend = lerEnvelopeErro(e)?.data + const existente = erroBackend?.response + if (erroBackend?.internalCode === COD_CERTIDAO_VALIDA_JA_EXISTE && existente?.id != null) { + certidaoExistente.value = existente + etapa.value = 'certidaoExistente' + return + } + mensagemErro.value = erroBackend?.description ?? extrairErro(e) ?? 'Erro ao gerar o PDF. Tente novamente.' } finally { carregandoEmissao.value = false }