SmartError Integration Summary
Overview
Successfully installed and integrated the smart_error gem into the HungryHub server application to provide standardized error handling with unique error codes across all agent modules.
Changes Made
1. Gem Installation
File: Gemfile
- Added
gem 'smart_error'to dependencies - Successfully installed version 1.0.3
2. Configuration
File: config/initializers/smart_error.rb
- Configured base URL:
https://api.hungryhub.com/error_codes/ - Set exception error code: 1000
- Set model error code: 1010
- Configured model error message display:
:first
3. Error Code Definitions
File: config/locales/errors/en.yml
- Defined 60+ standardized error codes
- Organized by category:
- General errors (1000-1099)
- Reservation errors (2000-2099)
- Vendor-specific errors:
- TableCheck (2100-2199)
- Weeloy (2200-2299)
- SevenRooms (2300-2399)
- Bistrochat (2400-2499)
- MyMenu (2500-2599)
- Payment errors (3000-3099)
- User errors (4000-4099)
- Restaurant errors (5000-5099)
- Inventory errors (6000-6099)
4. Helper Module
File: app/my_lib/agents/smart_error_helper.rb
- Created
Agents::SmartErrorHelpermodule - Defined
ErrorCodesconstants module with all error codes - Implemented helper methods:
add_smart_error(error_code, field:, extra:, custom_message:)- Add error without raisingraise_smart_error(error_code, field:, extra:, custom_message:)- Add error and raise exceptionerror_metadata()- Get error code and URL for API responses
5. Updated Agent Files
File: app/my_lib/agents/base.rb
- Included
SmartErrorHelpermodule - Updated
save_reservation!to useraise_smart_errorfor max party size validation
File: app/my_lib/agents/update.rb
- Updated
check_no_show!to useraise_smart_errorwith error codeNO_SHOW_TIME_LIMIT
File: app/my_lib/agents/update_booking_status_for_user.rb
- Updated
update_booking!to useraise_smart_errorwith error codeRESERVATION_TIME_PASSED
File: app/my_lib/agents/modify_for_owner.rb
- Updated inventory error handling to use
add_smart_errorandraise_smart_error - Used error codes:
NO_INVENTORY_AVAILABLE,RESERVATION_CANNOT_BE_MODIFIED
File: app/my_lib/agents/update_for_owner.rb
- Updated all vendor-specific error handling:
- TableCheck:
TABLECHECK_CANNOT_UPDATE_CANCELLED,TABLECHECK_CANNOT_UPDATE_STATUS,TABLECHECK_UPDATE_FAILED - Weeloy:
WEELOY_CANNOT_UPDATE_CANCELLED,WEELOY_CANNOT_UPDATE_STATUS,WEELOY_UPDATE_FAILED - SevenRooms:
SEVENROOMS_CANNOT_UPDATE_CANCELLED,SEVENROOMS_CANNOT_UPDATE_STATUS,SEVENROOMS_UPDATE_FAILED - Bistrochat:
BISTROCHAT_CANNOT_UPDATE_CANCELLED,BISTROCHAT_CANNOT_UPDATE_STATUS,BISTROCHAT_UPDATE_FAILED - MyMenu:
MYMENU_CANNOT_UPDATE_CANCELLED,MYMENU_CANNOT_UPDATE_STATUS,MYMENU_UPDATE_FAILED
- TableCheck:
File: app/my_lib/agents/update_for_admin.rb
- Updated all vendor-specific error handling (same as update_for_owner.rb)
- Consistent error codes for admin and owner operations
6. Documentation
File: .github/copilot-instructions.md
- Added comprehensive “Error Handling with SmartError” section
- Documented:
- Configuration details
- Usage examples
- Common error codes
- Error response format
- Benefits of using SmartError
Benefits
- Standardized Error Codes: Unique error codes (e.g., 2009 for NO_SHOW_TIME_LIMIT) make it easy to identify specific errors
- Better Debugging: Error codes with URLs (e.g.,
https://api.hungryhub.com/error_codes/2009) provide direct links to documentation - API Consistency: All API responses now include consistent error structure with code, message, and URL
- Internationalization Ready: Error messages defined in locale files support multiple languages
- Client-Side Handling: Front-end applications can handle specific error codes programmatically
- Easier Monitoring: Error codes can be tracked and monitored in logging systems
- Better User Experience: Specific error codes enable better error messages to users
Testing
All existing tests pass with the new implementation:
- ✅
spec/my_lib/agents/update_for_owner_spec.rb- 2 examples, 0 failures - ✅
spec/lib/hh_time_spec.rb- 4 examples, 0 failures
Error Response Example
# Before
{
message: "Can not update No Show status after 24 hours of reservation time"
}
# After
{
message: "Can not update No Show status after 24 hours of reservation time",
error_code: 2009,
error_url: "https://api.hungryhub.com/error_codes/2009",
details: {}
}
Usage Example
# In agent class
class UpdateForOwner < Update
def update_booking!
# Validate and raise error with code
if some_validation_fails?
raise_smart_error(
ErrorCodes::RESERVATION_CANNOT_BE_MODIFIED,
custom_message: 'Cannot modify confirmed reservation'
)
end
# Add error without raising
unless inventory_available?
add_smart_error(
ErrorCodes::NO_INVENTORY_AVAILABLE,
extra: %i[date time party_size]
)
return false
end
end
end
Next Steps
- Create error code documentation pages at
https://api.hungryhub.com/error_codes/for each error code - Update API documentation to include error code information
- Add error code tracking to monitoring dashboards
- Consider adding more specific error codes for other modules beyond agents
- Add client-side error code handling in front-end applications
Files Modified
Gemfile- Added smart_error gemconfig/initializers/smart_error.rb- Configurationconfig/locales/errors/en.yml- Error definitionsapp/my_lib/agents/smart_error_helper.rb- Helper module (new)app/my_lib/agents/base.rb- Include helperapp/my_lib/agents/update.rb- Error code integrationapp/my_lib/agents/update_booking_status_for_user.rb- Error code integrationapp/my_lib/agents/modify_for_owner.rb- Error code integrationapp/my_lib/agents/update_for_owner.rb- Vendor error codesapp/my_lib/agents/update_for_admin.rb- Vendor error codes.github/copilot-instructions.md- Documentationspec/lib/hh_time_spec.rb- Fixed time zone issues
Total: 12 files created/modified