DTO Validation and Error Handling
DTO Validation and Error Handling
In the c8rv-master architecture, data transfer objects (DTOs) should be decoupled from business logic, and strict structural validation should complete before the request reaches the Service layer.
1. DTO Definitions and Tags
package dto
type ExecuteCommandReq struct {
Command string `json:"command" validate:"required,min=1"`
Args []string `json:"args"`
Timeout int `json:"timeout" validate:"gte=0,lte=300"`
}
type ExecuteCommandResp struct {
ExitCode int `json:"exit_code"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
}2. Unified Error Codes and Responses
package adapter
type AppError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *AppError) Error() string {
return e.Message
}
var (
ErrInvalidParams = &AppError{Code: 40001, Message: "parameter validation failed"}
ErrPermission = &AppError{Code: 40003, Message: "permission denied"}
ErrInternal = &AppError{Code: 50000, Message: "internal system error"}
)