本文整理匯總了Golang中canopy/canolog.Error函數的典型用法代碼示例。如果您正苦於以下問題:Golang Error函數的具體用法?Golang Error怎麽用?Golang Error使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Error函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateAccount
func (conn *CassConnection) CreateAccount(
username,
email,
password string) (datalayer.Account, error) {
salt := conn.dl.cfg.OptPasswordSecretSalt()
hashCost := conn.dl.cfg.OptPasswordHashCost()
password_hash, _ := bcrypt.GenerateFromPassword(
[]byte(password+salt), int(hashCost))
err := validateUsername(username)
if err != nil {
return nil, err
}
err = validateEmail(email)
if err != nil {
return nil, err
}
err = validatePassword(password)
if err != nil {
return nil, err
}
activation_code, err := random.Base64String(24)
if err != nil {
return nil, err
}
now := time.Now()
// TODO: transactionize
if err := conn.session.Query(`
INSERT INTO accounts (
username,
email,
password_hash,
activated,
activation_code,
password_reset_code,
password_reset_code_expiry)
VALUES (?, ?, ?, ?, ?, ?, ?)
`, username, email, password_hash, false, activation_code, "", now).Exec(); err != nil {
canolog.Error("Error creating account:", err)
return nil, err
}
if err := conn.session.Query(`
INSERT INTO account_emails (email, username)
VALUES (?, ?)
`, email, username).Exec(); err != nil {
canolog.Error("Error setting account email:", err)
return nil, err
}
return &CassAccount{conn, username, email, password_hash, false, activation_code, "", now}, nil
}
示例2: LookupAccount
func (conn *CassConnection) LookupAccount(
usernameOrEmail string) (datalayer.Account, error) {
var account CassAccount
var username string
canolog.Info("Looking up account: ", usernameOrEmail)
if strings.Contains(usernameOrEmail, "@") {
canolog.Info("It is an email address")
// email address provided. Lookup username based on email
err := conn.session.Query(`
SELECT email, username FROM account_emails
WHERE email = ?
LIMIT 1
`, usernameOrEmail).Consistency(gocql.One).Scan(
&account.email, &username)
if err != nil {
canolog.Error("Error looking up account", err)
return nil, err
}
} else {
canolog.Info("It is not an email address")
username = usernameOrEmail
}
canolog.Info("fetching info for: ", username)
// Lookup account info based on username
err := conn.session.Query(`
SELECT
username,
email,
password_hash,
activated,
activation_code,
password_reset_code,
password_reset_code_expiry
FROM accounts
WHERE username = ?
LIMIT 1
`, username).Consistency(gocql.One).Scan(
&account.username,
&account.email,
&account.password_hash,
&account.activated,
&account.activation_code,
&account.password_reset_code,
&account.password_reset_code_expiry)
if err != nil {
canolog.Error("Error looking up account", err)
return nil, err
}
canolog.Info("Success")
account.conn = conn
return &account, nil
}
示例3: SetEmail
func (account *CassAccount) SetEmail(newEmail string) error {
// validate new email address
err := validateEmail(newEmail)
if err != nil {
return err
}
// generate new activation code
newActivationCode, err := random.Base64String(24)
if err != nil {
return err
}
// TODO: transactionize
// update accounts table
err = account.conn.session.Query(`
UPDATE accounts
SET email = ?,
activated = false,
activation_code = ?
WHERE username = ?
`, newEmail, newActivationCode, account.Username()).Exec()
if err != nil {
canolog.Error("Error changing email address to", newEmail, ":", err)
return err
}
// Update account_emails table
// Remove old email address
err = account.conn.session.Query(`
DELETE FROM account_emails
WHERE email = ?
`, account.Email()).Exec()
if err != nil {
canolog.Error("Error removing old email while changing email address to", newEmail, ":", err)
return err
}
// Add new email address
err = account.conn.session.Query(`
INSERT INTO account_emails (email, username)
VALUES (?, ?)
`, newEmail, account.Username()).Exec()
if err != nil {
canolog.Error("Error adding new email while changing email address to", newEmail, ":", err)
return err
}
// update local copy
account.activated = false
account.activation_code = newActivationCode
account.email = newEmail
return nil
}
示例4: DeleteAccount
func (conn *CassConnection) DeleteAccount(username string) error {
// TODO: We should archive the account, not actually delete it.
// TODO: If we are really deleting it, then we need to also cleanup
// all the other data (permissions, orphanded devices, etc).
account, err := conn.LookupAccount(username)
if err != nil {
canolog.Error("Error looking up account for deletion: ", err)
return err
}
email := account.Email()
// TODO: Transactionize. This might be done by adding a txn state field to
// the table.
err = conn.session.Query(`
DELETE FROM device_group
WHERE username = ?
`, username).Exec()
if err != nil {
canolog.Error("Error deleting account's device groups", err)
return err
}
err = conn.session.Query(`
DELETE FROM device_permissions
WHERE username = ?
`, username).Exec()
if err != nil {
canolog.Error("Error deleting account's permission", err)
return err
}
err = conn.session.Query(`
DELETE FROM account_emails
WHERE email = ?
`, email).Exec()
if err != nil {
canolog.Error("Error deleting account email", err)
return err
}
err = conn.session.Query(`
DELETE FROM accounts
WHERE username = ?
`, username).Exec()
if err != nil {
canolog.Error("Error deleting account", err)
return err
}
return nil
}
示例5: send
func (outbox *PigeonOutbox) send(hostname string, request *PigeonRequest, respChan chan<- Response) error {
resp := &PigeonResponse{}
// Dial the server
// TODO: Inefficient to dial each time?
canolog.Info("RPC Dialing")
rpcClient, err := rpc.DialHTTP("tcp", hostname+":1888")
if err != nil {
return fmt.Errorf("Pigeon: (dialing) %s", err.Error())
}
defer rpcClient.Close()
// Make the call
canolog.Info("RPC Calling")
err = rpcClient.Call("PigeonServer.RPCHandleRequest", request, resp)
if err != nil {
canolog.Error("Pigeon: (calling) ", err.Error())
// Send error response to channel
respChan <- resp
return fmt.Errorf("Pigeon: (calling) %s", err.Error())
}
// Send response to channel
respChan <- resp
return nil
}
示例6: LookupDevice
func (conn *CassConnection) LookupDevice(
deviceId gocql.UUID) (datalayer.Device, error) {
var device CassDevice
device.deviceId = deviceId
device.conn = conn
var last_seen time.Time
var ws_connected bool
err := conn.session.Query(`
SELECT friendly_name, location_note, secret_key, sddl, last_seen, ws_connected
FROM devices
WHERE device_id = ?
LIMIT 1`, deviceId).Consistency(gocql.One).Scan(
&device.name,
&device.locationNote,
&device.secretKey,
&device.docString,
&last_seen,
&ws_connected)
if err != nil {
canolog.Error(err)
return nil, err
}
// This scan returns Jan 1, 1970 UTC if last_seen is NULL.
if last_seen.Before(time.Unix(1, 0)) {
device.last_seen = nil
} else {
device.last_seen = &last_seen
}
device.wsConnected = ws_connected
if device.docString != "" {
device.doc, err = sddl.Sys.ParseDocumentString(device.docString)
if err != nil {
canolog.Error("Error parsing class string for device: ", device.docString, err)
return nil, err
}
} else {
device.doc = sddl.Sys.NewEmptyDocument()
}
return &device, nil
}
示例7: ExtendSDDL
func (device *CassDevice) ExtendSDDL(jsn map[string]interface{}) error {
// TODO: Race condition?
doc := device.SDDLDocument()
err := doc.Extend(jsn)
if err != nil {
canolog.Error("Error extending class ", jsn, err)
return err
}
// save modified SDDL class to DB
err = device.SetSDDLDocument(doc)
if err != nil {
canolog.Error("Error saving SDDL: ", err)
return err
}
return nil
}
示例8: ParseDocumentString
func (sys *SDDLSys) ParseDocumentString(doc string) (Document, error) {
var jsn map[string]interface{}
err := json.Unmarshal([]byte(doc), &jsn)
if err != nil {
canolog.Error("Error JSON decoding SDDL docoument: %s %s", doc, err)
return nil, err
}
return sys.ParseDocument(jsn)
}
示例9: LookupDeviceByStringID
func (conn *CassConnection) LookupDeviceByStringID(
id string) (datalayer.Device, error) {
deviceId, err := gocql.ParseUUID(id)
if err != nil {
canolog.Error(err)
return nil, err
}
return conn.LookupDevice(deviceId)
}
示例10: EraseDb
func (dl *CassDatalayer) EraseDb(keyspace string) error {
cluster := gocql.NewCluster("127.0.0.1")
session, err := cluster.CreateSession()
if err != nil {
canolog.Error("Error creating DB session: ", err)
return err
}
err = session.Query(`DROP KEYSPACE ` + keyspace + ``).Exec()
return err
}
示例11: PrepDb
func (dl *CassDatalayer) PrepDb(keyspace string) error {
cluster := gocql.NewCluster("127.0.0.1")
session, err := cluster.CreateSession()
if err != nil {
canolog.Error("Error creating DB session: ", err)
return err
}
// Create keyspace.
err = session.Query(`
CREATE KEYSPACE ` + keyspace + `
WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}
`).Exec()
if err != nil {
// Ignore errors (just log them).
canolog.Warn("(IGNORED) ", err)
}
// Create a new session connecting to that keyspace.
cluster = gocql.NewCluster("127.0.0.1")
cluster.Keyspace = keyspace
cluster.Consistency = gocql.Quorum
session, err = cluster.CreateSession()
if err != nil {
canolog.Error("Error creating DB session: ", err)
return err
}
// Perform all creation queries.
for _, query := range creationQueries {
if err := session.Query(query).Exec(); err != nil {
// Ignore errors (just print them).
// This allows PrepDB to be used to add new tables. Eventually, we
// should come up with a proper migration strategy.
canolog.Warn("(IGNORED) ", query, ": ", err)
}
}
return nil
}
示例12: DeleteDevice
func (conn *CassConnection) DeleteDevice(deviceId gocql.UUID) error {
// TODO: Should we archive the device, not actually delete it?
device, err := conn.LookupDevice(deviceId)
if err != nil {
canolog.Error("Error deleting device", err)
return err
}
err = conn.session.Query(`
DELETE FROM devices
WHERE device_id = ?
`, device.ID()).Exec()
if err != nil {
canolog.Error("Error deleting from devices table", err)
return err
}
// TODO: How to cleanup device_permissions?
// TODO: transactionize
// TODO: Cleanup cloud variable data
return nil
}
示例13: MigrateDB
func (dl *CassDatalayer) MigrateDB(keyspace, startVersion, endVersion string) error {
var err error
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = keyspace
session, err := cluster.CreateSession()
if err != nil {
canolog.Error("Error creating DB session: ", err)
return err
}
curVersion := startVersion
for curVersion != endVersion {
canolog.Info("Migrating from %s to next version", curVersion)
curVersion, err = dl.migrateNext(session, startVersion)
if err != nil {
canolog.Error("Failed migrating from %s:", curVersion, err)
return err
}
}
canolog.Info("Migration complete! DB is now version: %s", curVersion)
return nil
}
示例14: InsertSample
// Insert a cloud variable data sample.
func (device *CassDevice) InsertSample(varDef sddl.VarDef, t time.Time, value interface{}) error {
// Convert to UTC before inserting
t = t.UTC()
canolog.Info("Inserting sample", varDef.Name(), t)
// check last update time
lastUpdateTime, err := device.varLastUpdateTime(varDef.Name())
if err != nil {
canolog.Error("Error inserting sample:", err.Error())
return err
}
canolog.Info("Last update time was", lastUpdateTime)
if t.Before(lastUpdateTime) {
canolog.Error("Insertion time before last update time: ", t, lastUpdateTime)
return fmt.Errorf("Insertion time %s before last update time %s", t, lastUpdateTime)
}
// update last update time
err = device.varSetLastUpdateTime(varDef.Name(), t)
if err != nil {
return err
}
// For each LOD, insert or discard sample based on our
// stratification algorithm.
for lod := LOD_0; lod < LOD_END; lod++ {
err = device.insertOrDiscardSampleLOD(varDef, lastUpdateTime, lod, t, value)
if err != nil {
// TODO: Transactionize/rollback?
return err
}
}
// TODO: Do we need to update in-memory device object?
return nil
}
示例15: GetListeners
func (pigeonsys *CassPigeonSystem) GetListeners(key string) ([]string, error) {
var workers []string
rows, err := pigeonsys.conn.session.Query(`
SELECT * FROM listeners
WHERE key = ?
`, key).Consistency(gocql.One).Iter().SliceMap()
if err != nil {
canolog.Error(err)
}
if len(rows) != 1 {
return nil, fmt.Errorf("Expected 1 DB row for listener ", key)
}
workers = rows[0]["workers"].([]string)
return workers, nil
}