Openstatus www.openstatus.dev
at main 64 lines 1.6 kB view raw
1package server 2 3import ( 4 "errors" 5 "fmt" 6 7 "connectrpc.com/connect" 8 private_locationv1 "github.com/openstatushq/openstatus/apps/private-location/proto/private_location/v1" 9) 10 11// Validation errors 12var ( 13 ErrEmptyMonitorID = errors.New("monitor_id is required") 14 ErrEmptyID = errors.New("id is required") 15 ErrInvalidLatency = errors.New("latency must be non-negative") 16 ErrInvalidTimestamp = errors.New("timestamp must be positive") 17) 18 19// ValidateIngestHTTPRequest validates an HTTP ingest request 20func ValidateIngestHTTPRequest(req *private_locationv1.IngestHTTPRequest) error { 21 if req.MonitorId == "" { 22 return ErrEmptyMonitorID 23 } 24 if req.Latency < 0 { 25 return ErrInvalidLatency 26 } 27 if req.Timestamp <= 0 { 28 return ErrInvalidTimestamp 29 } 30 return nil 31} 32 33// ValidateIngestTCPRequest validates a TCP ingest request 34func ValidateIngestTCPRequest(req *private_locationv1.IngestTCPRequest) error { 35 if req.Id == "" { 36 return ErrEmptyID 37 } 38 if req.Latency < 0 { 39 return ErrInvalidLatency 40 } 41 if req.Timestamp <= 0 { 42 return ErrInvalidTimestamp 43 } 44 return nil 45} 46 47// ValidateIngestDNSRequest validates a DNS ingest request 48func ValidateIngestDNSRequest(req *private_locationv1.IngestDNSRequest) error { 49 if req.Id == "" { 50 return ErrEmptyID 51 } 52 if req.Latency < 0 { 53 return ErrInvalidLatency 54 } 55 if req.Timestamp <= 0 { 56 return ErrInvalidTimestamp 57 } 58 return nil 59} 60 61// NewValidationError creates a Connect error for validation failures 62func NewValidationError(err error) *connect.Error { 63 return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("validation error: %w", err)) 64}