本文整理汇总了Golang中github.com/mclarkson/obdi/external/ant0ine/go-json-rest/rest.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Logout
func (api *Api) Logout(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
session := Session{}
var errl error = nil
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 400)
return
}
mutex.Lock()
if err := api.db.Delete(&session).Error; err != nil {
rest.Error(w, err.Error(), 400)
mutex.Unlock()
return
}
mutex.Unlock()
logit("User '" + login + "' logged out")
api.LogActivity(session.Id, "User '"+login+"' logged out")
w.WriteJson("Success")
}
示例2: DeleteEnv
func (api *Api) DeleteEnv(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Delete
id := 0
if id, errl = strconv.Atoi(r.PathParam("id")); errl != nil {
rest.Error(w, "Invalid id.", 400)
return
}
env := Env{}
mutex.Lock()
if api.db.First(&env, id).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record not found.", 400)
return
}
mutex.Unlock()
mutex.Lock()
if err := api.db.Delete(&env).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
dc := Dc{}
mutex.Lock()
api.db.First(&dc, env.DcId)
mutex.Unlock()
text := fmt.Sprintf("Deleted environment '%s->%s'.",
dc.SysName, env.SysName)
api.LogActivity(session.Id, text)
w.WriteJson("Success")
}
示例3: GetAllEnvCaps
func (api *Api) GetAllEnvCaps(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
//session := Session{}
var errl error = nil
//if session,errl = api.CheckLogin( login, guid ); errl != nil {
if _, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
qs := r.URL.Query() // Query string - map[string][]string
envcaps := []EnvCap{}
if len(qs["code"]) > 0 {
srch := qs["code"][0]
mutex.Lock()
api.db.Order("code").Find(&envcaps, "code = ?", srch)
mutex.Unlock()
} else {
mutex.Lock()
err := api.db.Order("Code").Find(&envcaps)
mutex.Unlock()
if err.Error != nil {
if !err.RecordNotFound() {
rest.Error(w, err.Error.Error(), 500)
return
}
}
}
// Create a slice of maps from users struct
// to selectively copy database fields for display
u := make([]map[string]interface{}, len(envcaps))
for i := range envcaps {
u[i] = make(map[string]interface{})
u[i]["Id"] = envcaps[i].Id
u[i]["Code"] = envcaps[i].Code
u[i]["Desc"] = envcaps[i].Desc
}
// Too much noise
//api.LogActivity( session.Id, "Sent list of users" )
w.WriteJson(&u)
}
示例4: DeleteUser
// DeleteUser processes "DELETE /users" queries.
//
func (api *Api) DeleteUser(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Delete
id := 0
if id, errl = strconv.Atoi(r.PathParam("id")); errl != nil {
rest.Error(w, "Invalid id.", 400)
return
}
user := User{}
mutex.Lock()
if api.db.First(&user, id).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record not found.", 400)
return
}
mutex.Unlock()
mutex.Lock()
if err := api.db.Delete(&user).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
api.LogActivity(session.Id,
"Deleted user '"+user.Login+"'.")
w.WriteJson("Success")
}
示例5: AddJob
func (api *Api) AddJob(w rest.ResponseWriter, r *rest.Request) {
// Decode json post data into JobIn struct
logit(fmt.Sprintf("Connection from %s", r.RemoteAddr))
job := JobIn{}
if err := r.DecodeJsonPayload(&job); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if job.JobID == 0 {
rest.Error(w, "Incorrect data format received.", 400)
return
}
// Check the password matches
if job.Key != config.WorkerKey {
rest.Error(w, "Invalid key", 400)
return
}
// Add the job to the job list
api.AppendJob(job)
if api.Guid() == "" {
api.loginmutex.Lock()
if err := api.Login(); err != nil {
// Can't send this error to the Manager so must return it here
logit(fmt.Sprintf("Error: %s", err.Error()))
rest.Error(w, err.Error(), 400)
api.loginmutex.Unlock()
return
}
api.loginmutex.Unlock()
}
if err := api.sendStatus(job, JobOut{
Status: STATUS_NOTSTARTED,
StatusReason: "About to start job",
StatusPercent: 0,
Errors: 0,
}); err != nil {
logit(fmt.Sprintf("Error: %s", err.Error()))
}
w.WriteJson(job)
//a := fmt.Sprintf("%#v",job)
//logit(a)
go api.execCmd(job)
}
示例6: DeleteJob
func (api *Api) DeleteJob(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login == "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Delete
id := 0
if id, errl = strconv.Atoi(r.PathParam("id")); errl != nil {
rest.Error(w, "Invalid id.", 400)
return
}
job := Job{}
mutex.Lock()
if api.db.First(&job, id).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record not found.", 400)
return
}
mutex.Unlock()
mutex.Lock()
if err := api.db.Delete(&job).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
api.LogActivity(session.Id, fmt.Sprintf("Deleted job %d.", job.Id))
w.WriteJson(&job)
}
示例7: AddOutputLine
func (api *Api) AddOutputLine(w rest.ResponseWriter, r *rest.Request) {
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Admin is not allowed
if login == "admin" {
rest.Error(w, "Not allowed", 400)
return
}
// Check credentials
//session := Session{}
var errl error
if _, errl = api.CheckLoginNoExpiry(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
//defer api.TouchSession( guid )
outputLineData := OutputLine{}
if err := r.DecodeJsonPayload(&outputLineData); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if outputLineData.JobId == 0 {
rest.Error(w, "Incorrect data format received.", 400)
return
}
// Add OutputLine
mutex.Lock()
if err := api.db.Save(&outputLineData).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
//text := ""
//fmt.Sprintf( text,"%d",outputLineData.JobId )
//api.LogActivity( session.Id, "Started outputLine logging for job '"+
// text+"'." )
w.WriteJson("Success")
}
示例8: DeleteOutputLine
func (api *Api) DeleteOutputLine(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Admin is not allowed
if login == "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Delete
id := 0
if id, errl = strconv.Atoi(r.PathParam("id")); errl != nil {
rest.Error(w, "Invalid id.", 400)
return
}
outputline := OutputLine{}
mutex.Lock()
if err := api.db.Where("job_id = ?", id).Delete(&outputline).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
api.LogActivity(session.Id,
fmt.Sprintf("Deleted outputlines for job %d.", id))
w.WriteJson("Success")
}
示例9: AddPerm
func (api *Api) AddPerm(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Can't add if it exists already
permData := Perm{}
if err := r.DecodeJsonPayload(&permData); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if permData.UserId == 0 {
rest.Error(w, "Incorrect data format received.", 400)
return
}
perm := Perm{}
mutex.Lock()
if !api.db.Find(&perm, "env_id = ? and user_id = ?", permData.EnvId,
permData.UserId).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record exists.", 400)
return
}
mutex.Unlock()
// Add perm
mutex.Lock()
if err := api.db.Save(&permData).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
text := fmt.Sprintf("Added new environment permission. PermID = '%d'.",
permData.Id)
api.LogActivity(session.Id, text)
w.WriteJson(permData)
}
示例10: AddEnvCap
func (api *Api) AddEnvCap(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Can't add if it exists already
EnvCapData := EnvCap{}
if err := r.DecodeJsonPayload(&EnvCapData); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if len(EnvCapData.Code) == 0 || len(EnvCapData.Desc) == 0 {
rest.Error(w, "A required field is empty.", 400)
return
}
EnvCap := EnvCap{}
mutex.Lock()
if !api.db.Find(&EnvCap, "code = ?", EnvCapData.Code).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record exists.", 400)
return
}
mutex.Unlock()
// Add EnvCap
mutex.Lock()
if err := api.db.Save(&EnvCapData).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
text := fmt.Sprintf("Added new EnvCap, '%s'.",
EnvCapData.Code)
api.LogActivity(session.Id, text)
w.WriteJson(EnvCapData)
}
示例11: AddRepo
func (api *Api) AddRepo(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Can't add if it exists already
repoData := Repo{}
if err := r.DecodeJsonPayload(&repoData); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if len(repoData.Url) == 0 {
rest.Error(w, "Incorrect data format received.", 400)
return
}
repo := Repo{}
mutex.Lock()
if !api.db.Find(&repo, "Url = ?", repoData.Url).
RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record exists.", 400)
return
}
mutex.Unlock()
// Add repo
mutex.Lock()
if err := api.db.Save(&repoData).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
api.LogActivity(session.Id, "Added new repo '"+repoData.Url+"'.")
w.WriteJson(repoData)
}
示例12: GetAllEnvs
func (api *Api) GetAllEnvs(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Anyone can view envs
//session := Session{}
var errl error = nil
//if session,errl = api.CheckLogin( login, guid ); errl != nil {
if _, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
envs := []Env{}
qs := r.URL.Query() // Query string - map[string][]string
if login == "admin" {
if len(qs["sys_name"]) > 0 {
srch := qs["sys_name"][0]
if len(qs["dc_id"]) > 0 {
dcid := qs["dc_id"][0]
mutex.Lock()
api.db.Order("sys_name").Find(&envs,
"sys_name = ? and dc_id = ?", srch, dcid)
mutex.Unlock()
} else {
mutex.Lock()
api.db.Order("sys_name").Find(&envs, "sys_name = ?", srch)
mutex.Unlock()
}
/*
if api.db.Order("sys_name").
Find(&envs, "sys_name = ?", srch).RecordNotFound() {
rest.Error(w, "No results.", 400)
return
}
*/
} else {
mutex.Lock()
err := api.db.Order("dc_id,sys_name").Find(&envs)
mutex.Unlock()
if err.Error != nil {
if !err.RecordNotFound() {
rest.Error(w, err.Error.Error(), 500)
return
}
}
}
} else { //Not admin
// Only return readable/writeable envs for the current user
additional_where := ""
if len(qs["env_id"]) > 0 {
additional_where = "AND envs.id = " + qs["env_id"][0]
}
if len(qs["writeable"]) > 0 { // only writeable envs
mutex.Lock()
api.db.Where("envs.id in (SELECT perms.env_id from perms "+
"LEFT JOIN users on users.id=perms.user_id "+
"WHERE users.login=? and perms.writeable=1) "+
additional_where,
login).Find(&envs)
mutex.Unlock()
} else { // readable or writeable envs
mutex.Lock()
api.db.Where("envs.id in (SELECT perms.env_id from perms "+
"LEFT JOIN users on users.id=perms.user_id "+
"WHERE users.login=? and perms.enabled=1) "+
additional_where,
login).Find(&envs)
mutex.Unlock()
}
}
// Create a slice of maps from users struct
// to selectively copy database fields for display
u := make([]map[string]interface{}, len(envs))
for i := range envs {
u[i] = make(map[string]interface{})
u[i]["Id"] = envs[i].Id
u[i]["DispName"] = envs[i].DispName
u[i]["SysName"] = envs[i].SysName
//u[i]["WorkerIp"] = envs[i].WorkerIp
//u[i]["WorkerPort"] = envs[i].WorkerPort
u[i]["WorkerUrl"] = envs[i].WorkerUrl
if login == "admin" {
u[i]["WorkerKey"] = envs[i].WorkerKey
}
u[i]["CreatedAt"] = envs[i].CreatedAt
dc := Dc{}
//.........这里部分代码省略.........
示例13: GetAllUsers
// GetAllUsers processes "GET /users" queries.
//
func (api *Api) GetAllUsers(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
//session := Session{}
var errl error = nil
//if session,errl = api.CheckLogin( login, guid ); errl != nil {
if _, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
users := []User{}
qs := r.URL.Query() // map[string][]string
if len(qs["login"]) > 0 {
srch := qs["login"][0]
mutex.Lock()
if api.db.Order("login").
Find(&users, "login = ?", srch).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "No results.", 400)
return
}
mutex.Unlock()
} else {
mutex.Lock()
if api.db.Order("login").Find(&users).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Empty Table.", 400)
return
}
mutex.Unlock()
}
// Create a slice of maps from users struct
// to selectively copy database fields for display
u := make([]map[string]interface{}, len(users))
for i := range users {
u[i] = make(map[string]interface{})
u[i]["Id"] = users[i].Id
u[i]["Login"] = users[i].Login
u[i]["Forename"] = users[i].Forename
u[i]["Surname"] = users[i].Surname
u[i]["Enabled"] = users[i].Enabled
u[i]["CreatedAt"] = users[i].CreatedAt
u[i]["Email"] = users[i].Email
}
// Too much noise
//api.LogActivity( session.Id, "Sent list of users" )
w.WriteJson(&u)
}
示例14: AddDcCapMap
func (api *Api) AddDcCapMap(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Can't add if it exists already
dcCapMapData := DcCapMap{}
if err := r.DecodeJsonPayload(&dcCapMapData); err != nil {
rest.Error(w, "Invalid data format received.", 400)
return
} else if dcCapMapData.DcId == 0 || dcCapMapData.DcCapId == 0 {
rest.Error(w, "A required field is empty.", 400)
return
}
dcCapMap := DcCapMap{}
mutex.Lock()
if !api.db.Find(&dcCapMap, "dc_id = ? and dc_cap_id = ?",
dcCapMapData.DcId, dcCapMapData.DcCapId).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Record exists.", 400)
return
}
mutex.Unlock()
// Check that DcId and DcCapId exist
dc := Dc{}
mutex.Lock()
if api.db.Find(&dc, dcCapMapData.DcId).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Invalid data centre id.", 400)
return
}
mutex.Unlock()
dcCap := DcCap{}
mutex.Lock()
if api.db.Find(&dcCap, dcCapMapData.DcCapId).RecordNotFound() {
mutex.Unlock()
rest.Error(w, "Invalid data centre capability id.", 400)
return
}
mutex.Unlock()
// Add DcCapMap
mutex.Lock()
if err := api.db.Save(&dcCapMapData).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
text := fmt.Sprintf("Added new DcCapMap, '%d'.",
dcCapMapData.Id)
api.LogActivity(session.Id, text)
w.WriteJson(dcCapMapData)
}
示例15: UpdateUser
// UpdateUser processes "PUT /users" queries.
//
func (api *Api) UpdateUser(w rest.ResponseWriter, r *rest.Request) {
// Check credentials
login := r.PathParam("login")
guid := r.PathParam("GUID")
// Only admin is allowed
if login != "admin" {
rest.Error(w, "Not allowed", 400)
return
}
session := Session{}
var errl error
if session, errl = api.CheckLogin(login, guid); errl != nil {
rest.Error(w, errl.Error(), 401)
return
}
defer api.TouchSession(guid)
// Ensure user exists
id := r.PathParam("id")
// Check that the id string is a number
if _, err := strconv.Atoi(id); err != nil {
rest.Error(w, "Invalid id.", 400)
return
}
// Load data from db, then ...
user := User{}
mutex.Lock()
if api.db.Find(&user, id).RecordNotFound() {
mutex.Unlock()
//rest.Error(w, err.Error(), 400)
rest.Error(w, "Record not found.", 400)
return
}
mutex.Unlock()
// FIXME: DecodeJsonPayload(&somethingelse) then
// merge with 'user' manually. This will remove
// the 'password can't begin with $' limitation.
// ... overwrite any sent fields
if err := r.DecodeJsonPayload(&user); err != nil {
//rest.Error(w, err.Error(), 400)
rest.Error(w, "Invalid data format received.", 400)
return
}
// Add user
if !strings.HasPrefix(user.Passhash, "$") {
c := &Crypt{}
c.Pass = []byte(user.Passhash)
c.Crypt()
user.Passhash = string(c.Hash)
}
// Force the use of the path id over an id in the payload
Id, _ := strconv.Atoi(id)
user.Id = int64(Id)
mutex.Lock()
if err := api.db.Save(&user).Error; err != nil {
mutex.Unlock()
rest.Error(w, err.Error(), 400)
return
}
mutex.Unlock()
api.LogActivity(session.Id,
"Updated user details for '"+user.Login+"'.")
w.WriteJson(user)
}