2015-05-30 20:00:46 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2015-06-10 18:18:16 +00:00
|
|
|
"strings"
|
2015-05-30 20:00:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrFieldsRequired is returned when a point does not any fields.
|
|
|
|
ErrFieldsRequired = errors.New("fields required")
|
|
|
|
|
|
|
|
// ErrFieldTypeConflict is returned when a new field already exists with a different type.
|
|
|
|
ErrFieldTypeConflict = errors.New("field type conflict")
|
|
|
|
)
|
|
|
|
|
2015-10-07 13:38:30 +00:00
|
|
|
// ErrDatabaseNotFound indicates that a database operation failed on the
|
|
|
|
// specified database because the specified database does not exist.
|
2015-06-03 15:32:50 +00:00
|
|
|
func ErrDatabaseNotFound(name string) error { return fmt.Errorf("database not found: %s", name) }
|
2015-05-30 20:00:46 +00:00
|
|
|
|
2015-10-07 13:38:30 +00:00
|
|
|
// ErrRetentionPolicyNotFound indicates that the named retention policy could
|
|
|
|
// not be found in the database.
|
2015-09-17 01:31:03 +00:00
|
|
|
func ErrRetentionPolicyNotFound(name string) error {
|
|
|
|
return fmt.Errorf("retention policy not found: %s", name)
|
|
|
|
}
|
|
|
|
|
2015-10-06 04:10:25 +00:00
|
|
|
func errMeasurementNotFound(name string) error { return fmt.Errorf("measurement not found: %s", name) }
|
2015-05-30 20:00:46 +00:00
|
|
|
|
2015-10-06 04:10:25 +00:00
|
|
|
func errorf(format string, a ...interface{}) (err error) {
|
2015-05-30 20:00:46 +00:00
|
|
|
if _, file, line, ok := runtime.Caller(2); ok {
|
|
|
|
a = append(a, file, line)
|
|
|
|
err = fmt.Errorf(format+" (%s:%d)", a...)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf(format, a...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsClientError indicates whether an error is a known client error.
|
|
|
|
func IsClientError(err error) bool {
|
2015-06-10 18:18:16 +00:00
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
if err == ErrFieldsRequired {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err == ErrFieldTypeConflict {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:18:16 +00:00
|
|
|
if strings.Contains(err.Error(), ErrFieldTypeConflict.Error()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-30 20:00:46 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustMarshal encodes a value to JSON.
|
|
|
|
// This will panic if an error occurs. This should only be used internally when
|
|
|
|
// an invalid marshal will cause corruption and a panic is appropriate.
|
|
|
|
func mustMarshalJSON(v interface{}) []byte {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
panic("marshal: " + err.Error())
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustUnmarshalJSON decodes a value from JSON.
|
|
|
|
// This will panic if an error occurs. This should only be used internally when
|
|
|
|
// an invalid unmarshal will cause corruption and a panic is appropriate.
|
|
|
|
func mustUnmarshalJSON(b []byte, v interface{}) {
|
|
|
|
if err := json.Unmarshal(b, v); err != nil {
|
|
|
|
panic("unmarshal: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert will panic with a given formatted message if the given condition is false.
|
|
|
|
func assert(condition bool, msg string, v ...interface{}) {
|
|
|
|
if !condition {
|
|
|
|
panic(fmt.Sprintf("assert failed: "+msg, v...))
|
|
|
|
}
|
|
|
|
}
|