Modernization improvements: validation, logging, retry logic, and error handling#46
Modernization improvements: validation, logging, retry logic, and error handling#46heywinit wants to merge 7 commits intoniledatabase:alphafrom
Conversation
gwenshap
left a comment
There was a problem hiding this comment.
Thanks for all the validations. Overall, it looks like good improvements and we appreciate.
I had few comments around retries and validations.
src/lib/api.ts
Outdated
| retryDelay: axiosRetry.exponentialDelay, | ||
| retryCondition: (error) => { | ||
| const status = error.response?.status; | ||
| return !status || status === 408 || status === 429 || (status >= 500 && status < 600); |
There was a problem hiding this comment.
Any reason not to stick to the default retry condition for axiosRetry (isNetworkOrIdempotentRequestError)?
It seems safer (since error 500 is not always safe to retry). As far as I can see, the only status your code handles and isNetworkOrIdempotentRequestError does not is 408, and Nile doesn't return that.
There was a problem hiding this comment.
Fixed it using isNetworkOrIdempotentRequestError now
src/lib/validation.ts
Outdated
| return { valid: false, error: 'Database name is required' }; | ||
| } | ||
|
|
||
| if (name.length > 63) { |
There was a problem hiding this comment.
We actually don't limit database names in Nile
src/lib/validation.ts
Outdated
| } | ||
|
|
||
| if (!/^[a-z][a-z0-9_-]*$/.test(name)) { | ||
| return { valid: false, error: 'Database name must start with lowercase letter and contain only lowercase letters, numbers, underscores, and hyphens' }; |
src/lib/validation.ts
Outdated
| } | ||
|
|
||
| if (name.length > 255) { | ||
| return { valid: false, error: 'Tenant name must be 255 characters or less' }; |
src/lib/validation.ts
Outdated
| } | ||
|
|
||
| if (email.length > 255) { | ||
| return { valid: false, error: 'Email must be 255 characters or less' }; |
There was a problem hiding this comment.
We don't limit email length
|
Thanks for the quick review. My bad I wasn't aware of Nile's validation scope. I've fixed everything in fd878fc. Let me know if there are any other caveats I should take care of as well. |
|
I just realized that our CI/CD wasn't set to run the test action for external contributions. I fixed that in a separate PR. But, when I tested your PR locally (with |
PR Overview
This PR adds several infrastructure improvements to make the CLI more robust: input validation utilities, structured logging, API retry logic for resilience, and consolidated error handling.
Follow up to #45
Changes
Input Validation (
src/lib/validation.ts- new)validateDatabaseName()- validates DB names follow PostgreSQL naming rules (63 chars max, lowercase letters/numbers/underscores/hyphens, no reserved words)validateTenantName()- validates tenant names (255 char max)validateTenantId()- validates tenant IDsStructured Logging (
src/lib/logger.ts- new)info(),debug(),warn(),error()functions with timestamp, level, and message formattingAPI Retry Logic (
src/lib/api.ts)axios-retrywith exponential backoff (3 retries)Error Handling Refactor (
src/lib/errorHandling.ts)handleError()functionErrorContexttype for context-aware error messages (API, Database, Tenant, User)Lint Fixes (
src/commands/local.ts,.eslintrc.js)