Skip to content

Refactor: Structured logging and type safety improvements#9

Merged
BENZOOgataga merged 7 commits into
mainfrom
copilot/review-code-quality-and-style
Feb 15, 2026
Merged

Refactor: Structured logging and type safety improvements#9
BENZOOgataga merged 7 commits into
mainfrom
copilot/review-code-quality-and-style

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 15, 2026

Comprehensive code review identified error handling and type safety issues. No architectural problems found.

Error Handling & Logging

Replaced ad-hoc console logging with NestJS Logger service:

  • HttpErrorFilter: Unhandled exceptions now logged with structured context (URL, method, stack)
  • PrismaService: Database connection retries use Logger
  • Bootstrap: Startup failures use Logger
  • api-client: JSON parse failures logged in development instead of silently swallowed
// Before
console.error("API bootstrap failed", error);

// After
const logger = new Logger("Bootstrap");
logger.error("API bootstrap failed", error instanceof Error ? error.stack : error);

Type Safety

  • Created shared createPrismaTransactionMock() utility to consolidate duplicate test mock code
  • Added isPrismaErrorWithCode() type guard for Prisma error handling instead of unsafe assertions
// Before
const prismaCode = (error as { errorCode?: string }).errorCode;

// After
interface PrismaErrorWithCode extends Error {
  errorCode?: string;
}
function isPrismaErrorWithCode(error: unknown): error is PrismaErrorWithCode {
  return error instanceof Error && "errorCode" in error;
}

Code Quality Assessment

Reviewed for duplication and magic numbers:

  • URL normalization: Single-line operations, extraction not justified
  • Environment parsing: Already well-structured
  • Magic numbers: Contextually clear (basis points, comparisons)

Security

CodeQL scan: 0 vulnerabilities

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkpoint.prisma.io
    • Triggering command: /opt/hostedtoolcache/node/24.13.0/x64/bin/node /opt/hostedtoolcache/node/24.13.0/x64/bin/node /home/REDACTED/work/CorpSim/CorpSim/node_modules/.pnpm/prisma@6.19.2_typescript@5.9.3/node_modules/prisma/build/child {"product":"prisma","version":"6.19.2","cli_install_type":"local","information":"","local_timestamp":"2026-02-15T02:16:53Z","project_hash":"bb9ce37e","cli_path":"/home/REDACTED/work/CorpSim/CorpSim/packages/db/node_modules/prisma/build/index.js","cli_path_h (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits February 15, 2026 02:18
- Replace console.* with NestJS Logger service in API
- Add comprehensive error logging in HttpErrorFilter
- Improve JSON parsing error handling in api-client
- Add context (URL, method) to unhandled exception logs

Co-authored-by: BENZOOgataga <50145143+BENZOOgataga@users.noreply.github.com>
- Extract test mock utility to reduce code duplication
- Add proper type guard for Prisma errors
- Document intentional type assertion in test utilities

Co-authored-by: BENZOOgataga <50145143+BENZOOgataga@users.noreply.github.com>
Co-authored-by: BENZOOgataga <50145143+BENZOOgataga@users.noreply.github.com>
- Simplify logger.error() call to avoid duplicating exception message
- Structure context object with separate message and stack fields

Co-authored-by: BENZOOgataga <50145143+BENZOOgataga@users.noreply.github.com>
Copilot AI changed the title [WIP] Review codebase for style, logic, syntax, and quality Refactor: Structured logging and type safety improvements Feb 15, 2026
Copilot AI requested a review from BENZOOgataga February 15, 2026 02:25
@BENZOOgataga BENZOOgataga marked this pull request as ready for review February 15, 2026 02:36
Copilot AI review requested due to automatic review settings February 15, 2026 02:36
@BENZOOgataga BENZOOgataga merged commit dd57e0b into main Feb 15, 2026
8 checks passed
@BENZOOgataga BENZOOgataga deleted the copilot/review-code-quality-and-style branch February 15, 2026 02:36
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 15, 2026

Greptile Overview

Greptile Summary

Replaced ad-hoc console logging with NestJS Logger service across API bootstrap, Prisma connection retry logic, and HTTP error filter. Added structured error context (URL, method, stack traces) for unhandled exceptions. Improved type safety by creating a shared createPrismaTransactionMock() test utility and adding isPrismaErrorWithCode() type guard to eliminate unsafe type assertions. Enhanced error visibility in development with explicit JSON parse failure warnings in api-client.

The changes align with AGENTS.md's code style requirements: TypeScript strict mode, avoiding unsafe any types, and proper error handling. The release entry follows the required format with type patch, appropriate area api, and clear summary.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are purely refactoring improvements with no behavioral changes. All modifications improve code quality through structured logging, better type safety, and DRY principles. The logging additions are defensive (no changes to business logic), the type guards replace unsafe assertions, and the test utility consolidates duplicate code. No API contracts or interfaces were modified.
  • No files require special attention

Last reviewed commit: f75f2ab

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors error handling and logging across the codebase, replacing ad-hoc console logging with NestJS Logger service for structured logging, and improves type safety in tests by consolidating duplicate mock utilities.

Changes:

  • Replaced console logging with NestJS Logger in API bootstrap, Prisma service, and HTTP error filter
  • Added isPrismaErrorWithCode() type guard for safer Prisma error handling
  • Created shared createPrismaTransactionMock() test utility to eliminate code duplication
  • Enhanced JSON parse error logging in api-client for better debugging in development mode

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/sim/tests/test-utils.ts New shared test utility for creating Prisma transaction mocks with proper documentation
packages/sim/tests/workforce.test.ts Removed duplicate createPrismaTransactionMock implementation, now imports from test-utils
packages/sim/tests/research.test.ts Removed duplicate createPrismaTransactionMock implementation, now imports from test-utils
apps/web/src/lib/api-client.ts Enhanced JSON parse error handling with development-mode logging instead of silent failure
apps/api/src/prisma/prisma.service.ts Replaced console.warn with NestJS Logger and added type guard for Prisma errors
apps/api/src/main.ts Replaced console.error with NestJS Logger for bootstrap failures
apps/api/src/common/filters/http-error.filter.ts Added structured logging for unhandled exceptions with request context
.releases/unreleased/code-quality-improvements.md Patch changeset documenting error handling and type safety improvements

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +7 to +13
interface PrismaErrorWithCode extends Error {
errorCode?: string;
}

function isPrismaErrorWithCode(error: unknown): error is PrismaErrorWithCode {
return error instanceof Error && "errorCode" in error;
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Prisma error type guard only checks for 'errorCode', but other parts of the codebase show that Prisma errors can have either 'errorCode' or 'code' properties. The schema-readiness service checks both properties (schema-readiness.service.ts:76), and maintenance service uses 'code' (maintenance.service.ts:279). Consider updating the interface and type guard to handle both properties to ensure all Prisma connection errors are properly detected for retries.

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +74
this.logger.error("Unhandled exception", {
url: request?.url,
method: request?.method,
message: exception instanceof Error ? exception.message : String(exception),
stack: exception instanceof Error ? exception.stack : undefined
});
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NestJS Logger.error method signature expects a stack trace string as the second argument, not a context object. Consider restructuring this to pass the stack trace directly as the second parameter, or format it as a string. For example: this.logger.error('Unhandled exception at ${request?.method} ${request?.url}: ${message}', stack) where message and stack are extracted from the exception.

Suggested change
this.logger.error("Unhandled exception", {
url: request?.url,
method: request?.method,
message: exception instanceof Error ? exception.message : String(exception),
stack: exception instanceof Error ? exception.stack : undefined
});
const errorMessage = `Unhandled exception at ${request?.method ?? "UNKNOWN"} ${request?.url ?? "UNKNOWN"}: ${
exception instanceof Error ? exception.message : String(exception)
}`;
const stack = exception instanceof Error ? exception.stack : undefined;
this.logger.error(errorMessage, stack);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants