Add inquiry status transition error handling and validation improvements
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Introduced InvalidStatusTransitionError to handle invalid status transitions for inquiries, providing clearer error messages. - Updated UpdateInquiryStatusForm to make the Reason field optional and added logic to set a default reason if not provided. - Enhanced form validation tests to cover new status transition error scenarios and validation messages. - Refactored handler methods to utilize new error handling functions for improved response management. This update improves the robustness of inquiry status management by ensuring proper error handling and validation, enhancing user experience during status updates.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package inquiry
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Inquiry-specific errors
|
||||
var (
|
||||
@@ -25,6 +29,34 @@ func (e *DuplicateInquiryError) Error() string {
|
||||
}
|
||||
|
||||
// NewDuplicateInquiryError creates a new duplicate inquiry error
|
||||
// InvalidStatusTransitionError is returned when an inquiry status change is not allowed.
|
||||
type InvalidStatusTransitionError struct {
|
||||
CurrentStatus string
|
||||
NewStatus string
|
||||
Allowed []string
|
||||
}
|
||||
|
||||
func (e *InvalidStatusTransitionError) Error() string {
|
||||
if len(e.Allowed) == 0 {
|
||||
return fmt.Sprintf("Inquiry status is already %s and cannot be changed", e.CurrentStatus)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"Cannot change inquiry status from %s to %s. Allowed next statuses: %s",
|
||||
e.CurrentStatus,
|
||||
e.NewStatus,
|
||||
strings.Join(e.Allowed, ", "),
|
||||
)
|
||||
}
|
||||
|
||||
// NewInvalidStatusTransitionError creates a status transition error with allowed next statuses.
|
||||
func NewInvalidStatusTransitionError(currentStatus, newStatus string, allowed []string) *InvalidStatusTransitionError {
|
||||
return &InvalidStatusTransitionError{
|
||||
CurrentStatus: currentStatus,
|
||||
NewStatus: newStatus,
|
||||
Allowed: allowed,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDuplicateInquiryError(inquiryType, existingID, status string, createdAt int64) *DuplicateInquiryError {
|
||||
var message string
|
||||
if inquiryType == "email" {
|
||||
|
||||
Reference in New Issue
Block a user