Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 45 additions & 26 deletions app/api/v1/process/[number]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetcher from "@/lib/utils/fetcher"
import { NextResponse } from "next/server"
import { NextRequest, NextResponse } from "next/server"
import { getCurrentUser } from "@/lib/user"
import { CargaDeConteudoEnum, obterDadosDoProcesso2 } from "@/lib/proc/process"
import { UnauthorizedError, withErrorHandler } from "@/lib/utils/api-error"

export const maxDuration = 60
// export const runtime = 'edge'
Expand Down Expand Up @@ -32,34 +32,53 @@ export const maxDuration = 60
* application/json:
* schema:
* type: object
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* example: Unauthorized
* 500:
* description: Internal Server Error
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* example: An internal server error occurred
*/
export async function GET(
req: Request,
async function GET_HANDLER(
req: NextRequest,
props: { params: Promise<{ number: string, piece: string }> }
) {
const params = await props.params;
const pUser = getCurrentUser()
const user = await pUser
if (!user) return Response.json({ errormsg: 'Unauthorized' }, { status: 401 })

try {
const url = new URL(req.url)
const kind = url.searchParams.get('kind')
const obterConteudo = url.searchParams.get('selectedPiecesContent') === 'true'
// if (kind) {
// const dadosDoProcesso = await obterDadosDoProcesso({
// numeroDoProcesso: params.number, pUser, kind,
// conteudoDasPecasSelecionadas: obterConteudo ? CargaDeConteudoEnum.SINCRONO : CargaDeConteudoEnum.NAO
// })
// return Response.json(dadosDoProcesso)
// }
const dadosDoProcesso = await obterDadosDoProcesso2({
numeroDoProcesso: params.number, pUser,
conteudoDasPecasSelecionadas: obterConteudo ? CargaDeConteudoEnum.SINCRONO : CargaDeConteudoEnum.NAO
})
return Response.json(dadosDoProcesso)
} catch (error) {
const message = fetcher.processError(error)
return NextResponse.json({ message: `${message}` }, { status: 405 });
if (!user) {
throw new UnauthorizedError();
}
}

const url = new URL(req.url)
const kind = url.searchParams.get('kind')
const obterConteudo = url.searchParams.get('selectedPiecesContent') === 'true'
// if (kind) {
// const dadosDoProcesso = await obterDadosDoProcesso({
// numeroDoProcesso: params.number, pUser, kind,
// conteudoDasPecasSelecionadas: obterConteudo ? CargaDeConteudoEnum.SINCRONO : CargaDeConteudoEnum.NAO
// })
// return Response.json(dadosDoProcesso)
// }
const dadosDoProcesso = await obterDadosDoProcesso2({
numeroDoProcesso: params.number, pUser,
conteudoDasPecasSelecionadas: obterConteudo ? CargaDeConteudoEnum.SINCRONO : CargaDeConteudoEnum.NAO
})
return NextResponse.json(dadosDoProcesso)
}

export const GET = withErrorHandler(GET_HANDLER)
69 changes: 69 additions & 0 deletions lib/utils/api-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { NextRequest, NextResponse } from 'next/server';

/**
* A standardized error response format for the API.
* @param message The error message.
* @param status The HTTP status code.
* @returns A NextResponse object with a standardized error body.
*/
export function apiErrorResponse(message: string, status: number) {
return NextResponse.json({ error: message }, { status });
}

/**
* Base class for custom API errors.
* Allows bundling a message and an HTTP status code together.
*/
export class ApiError extends Error {
public readonly status: number;
constructor(message: string, status: number) {
super(message);
this.status = status;
}
}

export class NotFoundError extends ApiError {
constructor(message: string = 'Resource not found') {
super(message, 404);
}
}

export class BadRequestError extends ApiError {
constructor(message: string = 'Bad request') {
super(message, 400);
}
}

export class UnauthorizedError extends ApiError {
constructor(message: string = 'Unauthorized') {
super(message, 401);
}
}

// A type for API handlers to ensure consistency.
// Note: 'props' is 'any' to accommodate different route parameter structures.
type ApiHandler = (req: NextRequest, props: any) => Promise<NextResponse>;

/**
* A higher-order function to wrap API route handlers with standardized error handling.
* @param handler The API route handler function to wrap.
* @returns A new handler function with built-in try-catch logic.
*/
export function withErrorHandler(handler: ApiHandler): ApiHandler {
return async (req: NextRequest, props: any) => {
try {
return await handler(req, props);
} catch (error) {
if (error instanceof ApiError) {
return apiErrorResponse(error.message, error.status);
}

// For unknown or unexpected errors, log them for debugging purposes.
// In a real production environment, you would use a dedicated logging service.
console.error('An unexpected error occurred:', error);

// Return a generic 500 response to avoid leaking implementation details.
return apiErrorResponse('An internal server error occurred', 500);
}
};
}
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.