package hypixel import ( "errors" "fmt" "time" ) // Errors: // SomeError - Couldn't reach the endpoint (maybe internet issues or blocked from hypxiel? or cloudflare is down, etc) // APIKeyInvalid - Thrown when the key is invalid // APIKeyRateLimited - Thrown when you're being rate limited (reached your call limit) // PlayerNotFoundError - When hypxiel can't find the target player // MalformedUuid - When hypixel returns a malformed UUID error // MalformedPlayer - When hypixel returns a malformed player error // PlayerNotFound - When a target player isn't found (usually by uuid) // TimeOutError - When the api call takes longer than the specified timeout to return // JsonUnmartialingError - When custom unmartialing functions throw errors (NOT a wrapper for generic json unmartial error) // UnknownError - When something that isn't already here happens // SomeError is thrown when the endpoint cannot be reached (e.g., internet issues, blocked from Hypixel, or Cloudflare is down) type SomeError struct { URL string Cause string } // NewSomeError creates a new instance of SomeError func NewSomeError(url, cause string) *SomeError { return &SomeError{ URL: url, Cause: cause, } } // Error returns the error message for SomeError func (e *SomeError) Error() string { return fmt.Sprintf("Unable to reach endpoint: URL: '%s', Cause: '%s'", e.URL, e.Cause) } // IsSomeError checks if the error is a SomeError func IsSomeError(err error) bool { var targetErr *SomeError return errors.As(err, &targetErr) } // APIKeyInvalidException is thrown when the HypixelApi key is invalid type APIKeyInvalidException struct { APIKey string Cause string } // NewAPIKeyInvalidException creates a new instance of APIKeyInvalidException func NewAPIKeyInvalidException(apiKey, cause string) *APIKeyInvalidException { return &APIKeyInvalidException{ APIKey: apiKey, Cause: cause, } } // PlayerNotFoundException is thrown when the HypixelApi cannot find the target player type PlayerNotFoundException struct { URL string PlayerID string Cause string } // NewPlayerNotFoundException helper function to create PlayerNotFoundException error func NewPlayerNotFoundException(url, id, cause string) *PlayerNotFoundException { return &PlayerNotFoundException{ URL: url, PlayerID: id, Cause: cause, } } // Error returns the error message for PlayerNotFoundException func (e *PlayerNotFoundException) Error() string { return fmt.Sprintf("Player not found: Player ID: '%s', fetch cause: '%s', fetched at URL: '%s'", e.PlayerID, e.Cause, e.URL) } // IsPlayerNotFoundException checks if the error is a PlayerNotFoundException error func IsPlayerNotFoundException(err error) bool { var targetErr *PlayerNotFoundException return errors.As(err, &targetErr) } // Error returns the error message for APIKeyInvalidException func (e *APIKeyInvalidException) Error() string { return fmt.Sprintf("HypixelApi key invalid: Key: '%s', Cause: '%s'", e.APIKey, e.Cause) } // IsAPIKeyInvalidException checks if the error is an APIKeyInvalidException func IsAPIKeyInvalidException(err error) bool { var targetErr *APIKeyInvalidException return errors.As(err, &targetErr) } // APIKeyRateLimitedException is thrown when the HypixelApi key has reached its call limit type APIKeyRateLimitedException struct { APIKey string RateLimit int Cause string } // NewAPIKeyRateLimitedException creates a new instance of APIKeyRateLimitedException func NewAPIKeyRateLimitedException(apiKey string, rateLimit int, cause string) *APIKeyRateLimitedException { return &APIKeyRateLimitedException{ APIKey: apiKey, RateLimit: rateLimit, Cause: cause, } } // Error returns the error message for APIKeyRateLimitedException func (e *APIKeyRateLimitedException) Error() string { return fmt.Sprintf("HypixelApi key rate limited: Key: '%s', Rate Limit: '%d', Cause: '%s'", e.APIKey, e.RateLimit, e.Cause) } // IsAPIKeyRateLimitedException checks if the error is an APIKeyRateLimitedException func IsAPIKeyRateLimitedException(err error) bool { var targetErr *APIKeyRateLimitedException return errors.As(err, &targetErr) } // MalformedUUIDException is thrown when Hypixel returns a malformed UUID error type MalformedUUIDException struct { UUID string Cause string } // NewMalformedUUIDException creates a new instance of MalformedUUIDException func NewMalformedUUIDException(uuid, cause string) *MalformedUUIDException { return &MalformedUUIDException{ UUID: uuid, Cause: cause, } } // Error returns the error message for MalformedUUIDException func (e *MalformedUUIDException) Error() string { return fmt.Sprintf("Malformed UUID: UUID: '%s', Cause: '%s'", e.UUID, e.Cause) } // IsMalformedUUIDException checks if the error is a MalformedUUIDException func IsMalformedUUIDException(err error) bool { var targetErr *MalformedUUIDException return errors.As(err, &targetErr) } // MalformedPlayerException is thrown when Hypixel returns a malformed player error type MalformedPlayerException struct { PlayerData string Cause string } // NewMalformedPlayerException creates a new instance of MalformedPlayerException func NewMalformedPlayerException(playerData, cause string) *MalformedPlayerException { return &MalformedPlayerException{ PlayerData: playerData, Cause: cause, } } // Error returns the error message for MalformedPlayerException func (e *MalformedPlayerException) Error() string { return fmt.Sprintf("Malformed player data: Data: '%s', Cause: '%s'", e.PlayerData, e.Cause) } // IsMalformedPlayerException checks if the error is a MalformedPlayerException func IsMalformedPlayerException(err error) bool { var targetErr *MalformedPlayerException return errors.As(err, &targetErr) } // TimeOutException is thrown when the HypixelApi call takes longer than the specified timeout to return type TimeOutException struct { Duration time.Duration URL string Cause string } // NewTimeOutException creates a new instance of TimeOutException func NewTimeOutException(duration time.Duration, url, cause string) *TimeOutException { return &TimeOutException{ Duration: duration, URL: url, Cause: cause, } } // Error returns the error message for TimeOutException func (e *TimeOutException) Error() string { return fmt.Sprintf("Request timed out after '%s' when accessing URL '%s': Cause: '%s'", e.Duration, e.URL, e.Cause) } // IsTimeOutException checks if the error is a TimeOutException func IsTimeOutException(err error) bool { var targetErr *TimeOutException return errors.As(err, &targetErr) } // JSONUnmarshallingException is thrown when custom unmarshalling functions encounter errors type JSONUnmarshallingException struct { Data string Cause string } // NewJSONUnmarshallingException creates a new instance of JSONUnmarshallingException func NewJSONUnmarshallingException(data, cause string) *JSONUnmarshallingException { return &JSONUnmarshallingException{ Data: data, Cause: cause, } } // Error returns the error message for JSONUnmarshallingException func (e *JSONUnmarshallingException) Error() string { return fmt.Sprintf("JSON unmarshalling error: Data: '%s', Cause: '%s'", e.Data, e.Cause) } // IsJSONUnmarshallingException checks if the error is a JSONUnmarshallingException func IsJSONUnmarshallingException(err error) bool { var targetErr *JSONUnmarshallingException return errors.As(err, &targetErr) } // UnknownErrorException is thrown when an unspecified error occurs type UnknownErrorException struct { Cause string } // NewUnknownErrorException creates a new instance of UnknownErrorException func NewUnknownErrorException(cause string) *UnknownErrorException { return &UnknownErrorException{ Cause: cause, } } // Error returns the error message for UnknownErrorException func (e *UnknownErrorException) Error() string { return fmt.Sprintf("Unknown error occurred: Cause: '%s'", e.Cause) } // IsUnknownErrorException checks if the error is an UnknownErrorException func IsUnknownErrorException(err error) bool { var targetErr *UnknownErrorException return errors.As(err, &targetErr) }