Skip to content

Commit da35572

Browse files
committed
refactor: exceptions personalizadas para o processamento de documentos
1 parent 0efb573 commit da35572

8 files changed

Lines changed: 47 additions & 10 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { HttpException, HttpStatus } from '@nestjs/common'
2+
3+
export class DocumentoConteudoInvalidoException extends HttpException {
4+
constructor() {
5+
super('Documento não contém conteúdo válido', HttpStatus.BAD_REQUEST)
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { HttpException, HttpStatus } from '@nestjs/common'
2+
3+
export class DocumentoPdfInvalidoException extends HttpException {
4+
constructor(tipoDocumento: string) {
5+
super(
6+
`O arquivo enviado não é um PDF válido. Tipo recebido: ${tipoDocumento}`,
7+
HttpStatus.BAD_REQUEST,
8+
)
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { HttpException, HttpStatus } from '@nestjs/common'
2+
3+
export class DocumentoTituloInvalidoException extends HttpException {
4+
constructor() {
5+
super('Documento não contém título válido', HttpStatus.BAD_REQUEST)
6+
}
7+
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NestFactory } from '@nestjs/core'
2-
import { AppModule } from './app.module'
2+
import { AppModule } from '@/modules/app.module'
33
import { Logger, NotFoundException, ValidationPipe } from '@nestjs/common'
44
import { ConfigService } from '@nestjs/config'
55

src/services/__tests__/unit/pdf-processing.service.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Test, TestingModule } from '@nestjs/testing'
22
import { PdfProcessingService } from '../../pdf-processing.service'
3-
import { BadRequestException } from '@nestjs/common'
43
import * as pdfParse from 'pdf-parse'
54
import { DocumentModule } from '@/modules/document.module'
65
import { ClientModule } from '@/modules/client.module'
6+
import { DocumentoPdfInvalidoException } from '@/errors/document/documento-pdf-invalido.exception'
7+
import { DocumentoConteudoInvalidoException } from '@/errors/document/documento-conteudo-invalido.exception'
78

89
jest.mock('pdf-parse', () => ({
910
__esModule: true,
@@ -61,7 +62,7 @@ describe('PdfProcessingService', () => {
6162
} as Express.Multer.File
6263

6364
await expect(service.extractTitleAndContent(file)).rejects.toThrow(
64-
BadRequestException,
65+
DocumentoPdfInvalidoException,
6566
)
6667
})
6768

@@ -77,7 +78,7 @@ describe('PdfProcessingService', () => {
7778
mockPdfParse(pdfData)
7879

7980
await expect(service.extractTitleAndContent(file)).rejects.toThrow(
80-
BadRequestException,
81+
DocumentoConteudoInvalidoException,
8182
)
8283
})
8384
})

src/services/pdf-processing.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
import { BadRequestException, Injectable } from '@nestjs/common'
1+
import { Injectable } from '@nestjs/common'
22
import pdfParse from 'pdf-parse'
33
import ProcessingDocument from '../contracts/processing-document.interface'
44
import { ExtractedDocumentProps } from '@/contracts/extracted-document.props'
5+
import { DocumentoConteudoInvalidoException } from '@/errors/document/documento-conteudo-invalido.exception'
6+
import { DocumentoTituloInvalidoException } from '@/errors/document/documento-titulo-invalido.exception'
7+
import { DocumentoPdfInvalidoException } from '@/errors/document/documento-pdf-invalido.exception'
58

69
@Injectable()
710
export class PdfProcessingService implements ProcessingDocument {
811
async extractTitleAndContent(
912
file: Express.Multer.File,
1013
): Promise<ExtractedDocumentProps> {
1114
if (!file.mimetype || file.mimetype !== 'application/pdf') {
12-
throw new BadRequestException(
13-
`O arquivo enviado não é um PDF válido. Tipo recebido: ${file.mimetype}`,
14-
)
15+
throw new DocumentoPdfInvalidoException(file.mimetype)
1516
}
1617

1718
const data = await pdfParse(file.buffer)
1819
const title = data.info.Title || 'Título não encontrado'
1920
const content = data.text
2021

2122
if (!content || content.trim() === '') {
22-
throw new BadRequestException('O PDF não contém conteúdo válido.')
23+
throw new DocumentoConteudoInvalidoException()
2324
}
2425

2526
if (!title || title.trim() === 'Título não encontrado') {
26-
throw new BadRequestException('O PDF não contém um título válido.')
27+
throw new DocumentoTituloInvalidoException()
2728
}
2829

2930
return { title, content } as ExtractedDocumentProps

src/services/web-processing.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { HttpService } from '@nestjs/axios'
55
import { Injectable } from '@nestjs/common'
66
import { lastValueFrom } from 'rxjs'
77
import * as cheerio from 'cheerio'
8+
import { DocumentoTituloInvalidoException } from '@/errors/document/documento-titulo-invalido.exception'
9+
import { DocumentoConteudoInvalidoException } from '@/errors/document/documento-conteudo-invalido.exception'
810

911
@Injectable()
1012
export class WebProcessingService implements ProcessingDocument {
@@ -16,6 +18,15 @@ export class WebProcessingService implements ProcessingDocument {
1618

1719
const title = $('title').text()
1820
const content = $('body').text()
21+
22+
if (!content || content.trim() === '') {
23+
throw new DocumentoConteudoInvalidoException()
24+
}
25+
26+
if (!title || title.trim() === 'Título não encontrado') {
27+
throw new DocumentoTituloInvalidoException()
28+
}
29+
1930
return { title, content }
2031
}
2132
}

0 commit comments

Comments
 (0)