Skip to content

fix(api): define missing ApiErrorCodes constants used by error_response#146

Open
agbid wants to merge 1 commit into
evolution-foundation:mainfrom
agbid:fix/api-error-codes-missing-constants
Open

fix(api): define missing ApiErrorCodes constants used by error_response#146
agbid wants to merge 1 commit into
evolution-foundation:mainfrom
agbid:fix/api-error-codes-missing-constants

Conversation

@agbid

@agbid agbid commented Jun 13, 2026

Copy link
Copy Markdown

Summary

  • 12 error codes referenced as ApiErrorCodes::XXX across 11+ controllers (pipeline_items, contacts, pipelines, labels, teams, macros, automation_rules, custom_filters, notifications, custom_attribute_definitions, dashboard_apps, webhooks, agent_bots, inboxes, pipeline_tasks, integration hooks) were used but never defined as constants in ApiErrorCodes.
  • Evaluating ApiErrorCodes::XXX for an undefined constant raises NameError before error_response even runs, turning what should be a clean 404/405/409/422 into an HTTP 500 INTERNAL_ERROR.
  • Found while testing the AI agent's pipeline_manipulation tool: add_to_pipeline (POST /pipelines/:id/pipeline_items) crashed with NameError - uninitialized constant ApiErrorCodes::CONVERSATION_NOT_FOUND whenever the lookup conversation wasn't found.
  • Added the 12 missing constants (CONTACT_NOT_FOUND, CONVERSATION_NOT_FOUND, TEAM_NOT_FOUND, LABEL_NOT_FOUND, AUTOMATION_RULE_NOT_FOUND, MACRO_NOT_FOUND, CUSTOM_FILTER_NOT_FOUND, NOTIFICATION_NOT_FOUND, CUSTOM_ATTRIBUTE_NOT_FOUND, METHOD_NOT_ALLOWED, CANNOT_DELETE_RESOURCE, OPERATION_FAILED) to app/models/concerns/api_error_codes.rb.
  • Added status: :not_found to the two pipeline_items_controller.rb "not found" responses (conversation/contact), matching the pattern used by every other *_NOT_FOUND response in the codebase.

Test plan

  • ruby -c syntax check on both changed files
  • Verify POST /api/v1/pipelines/:id/pipeline_items with a non-existent item_id now returns 404 CONVERSATION_NOT_FOUND / 404 CONTACT_NOT_FOUND instead of 500 INTERNAL_ERROR
  • Verify PATCH /api/v1/pipelines/:id/pipeline_items/:id/move_to_stage failure path (OPERATION_FAILED) returns 422 instead of 500

🤖 Generated with Claude Code

Summary by Sourcery

Define missing API error codes and ensure pipeline item not-found errors return 404 instead of 500.

Bug Fixes:

  • Add missing ApiErrorCodes constants referenced across controllers to prevent NameError and HTTP 500 responses.
  • Update pipeline_items_controller not-found responses for conversations and contacts to use 404 status via error_response.

12 error codes referenced via ApiErrorCodes::XXX across controllers
(pipeline_items, contacts, pipelines, labels, teams, macros,
automation_rules, custom_filters, notifications, custom_attribute
definitions, dashboard_apps, webhooks, agent_bots, inboxes, etc.) were
never defined as constants, so evaluating ApiErrorCodes::XXX raised
NameError before error_response could run — turning intended 404/409/422
responses into HTTP 500s. Also add status: :not_found to the two
pipeline_items_controller "not found" responses to match sibling
controllers.
@sourcery-ai

sourcery-ai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Reviewer's Guide

Defines previously-missing ApiErrorCodes constants used across controllers and aligns pipeline_items not-found responses with existing patterns so they return 404s instead of 500s.

Sequence diagram for pipeline_items create with missing conversation/contact

sequenceDiagram
  actor Client
  participant PipelineItemsController

  Client->>PipelineItemsController: create
  PipelineItemsController->>PipelineItemsController: [conversation is nil]
  PipelineItemsController->>PipelineItemsController: error_response(ApiErrorCodes::CONVERSATION_NOT_FOUND, message, status: :not_found)
  PipelineItemsController-->>Client: 404 CONVERSATION_NOT_FOUND

  Client->>PipelineItemsController: create
  PipelineItemsController->>PipelineItemsController: [contact is nil]
  PipelineItemsController->>PipelineItemsController: error_response(ApiErrorCodes::CONTACT_NOT_FOUND, message, status: :not_found)
  PipelineItemsController-->>Client: 404 CONTACT_NOT_FOUND
Loading

File-Level Changes

Change Details Files
Define missing ApiErrorCodes constants for various resource-not-found, method-not-allowed, conflict, and operation-failed cases used by controllers.
  • Add specific 404 constants for contact, conversation, team, label, automation rule, macro, custom filter, notification, and custom attribute not found cases
  • Introduce METHOD_NOT_ALLOWED constant under a dedicated 405 section
  • Add CANNOT_DELETE_RESOURCE to the 409 conflict error codes
  • Add OPERATION_FAILED to the 422 business-rule violation error codes
  • Maintain existing categorization/comments by HTTP status while inserting the new constants in appropriate sections
app/models/concerns/api_error_codes.rb
Ensure pipeline_items create error responses for missing conversation/contact return 404 instead of causing NameError or defaulting to non-not-found status.
  • Use ApiErrorCodes::CONVERSATION_NOT_FOUND and ApiErrorCodes::CONTACT_NOT_FOUND for missing conversation/contact in create action
  • Pass explicit status: :not_found to error_response for both missing conversation and missing contact branches
  • Preserve existing control flow by returning immediately after error_response calls
app/controllers/api/v1/pipeline_items_controller.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant