本文整理匯總了Golang中github.com/MG-RAST/golib/goweb.Context.RespondWithError方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.RespondWithError方法的具體用法?Golang Context.RespondWithError怎麽用?Golang Context.RespondWithError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/MG-RAST/golib/goweb.Context
的用法示例。
在下文中一共展示了Context.RespondWithError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Read
// GET: /client/{id}
func (cr *ClientController) Read(id string, cx *goweb.Context) {
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("heartbeat") { //handle heartbeat
hbmsg, err := core.QMgr.ClientHeartBeat(id)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData(hbmsg)
}
return
}
LogRequest(cx.Request) //skip heartbeat in access log
client, err := core.QMgr.GetClient(id)
if err != nil {
if err.Error() == e.ClientNotFound {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
} else {
logger.Error("Error in GET client:" + err.Error())
cx.RespondWithError(http.StatusBadRequest)
}
return
}
cx.RespondWithData(client)
}
示例2: UpdateMany
// PUT: /logger
func (cr *LoggerController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// must be admin user
if u == nil || u.Admin == false {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
// currently can only reset debug level
if query.Has("debug") {
levelStr := query.Value("debug")
levelInt, err := strconv.Atoi(levelStr)
if err != nil {
cx.RespondWithErrorMessage("invalid debug level: "+err.Error(), http.StatusBadRequest)
}
conf.DEBUG_LEVEL = levelInt
logger.Event(event.DEBUG_LEVEL, "level="+levelStr+";user="+u.Username)
cx.RespondWithData(map[string]int{"debuglevel": conf.DEBUG_LEVEL})
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例3: DeleteMany
// DELETE: /job?suspend, /job?zombie
func (cr *JobController) DeleteMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// If no auth was provided, and anonymous delete is allowed, use the public user
if u == nil {
if conf.ANON_DELETE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("suspend") {
num := core.QMgr.DeleteSuspendedJobsByUser(u)
cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num))
} else if query.Has("zombie") {
num := core.QMgr.DeleteZombieJobsByUser(u)
cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num))
} else {
cx.RespondWithError(http.StatusNotImplemented)
}
return
}
示例4: Update
// PUT: /client/{id} -> status update
func (cr *ClientController) Update(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("subclients") { //update the number of subclients for a proxy
if count, err := strconv.Atoi(query.Value("subclients")); err != nil {
cx.RespondWithError(http.StatusNotImplemented)
} else {
core.QMgr.UpdateSubClients(id, count)
cx.RespondWithData("ok")
}
return
}
if query.Has("suspend") { //resume the suspended client
if err := core.QMgr.SuspendClient(id); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client suspended")
}
return
}
if query.Has("resume") { //resume the suspended client
if err := core.QMgr.ResumeClient(id); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client resumed")
}
return
}
cx.RespondWithError(http.StatusNotImplemented)
}
示例5: UpdateMany
// PUT: /client
func (cr *ClientController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// If no auth was provided, and anonymous read is allowed, use the public user
if u == nil {
if conf.ANON_WRITE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("resumeall") { //resume the suspended client
num := core.QMgr.ResumeSuspendedClientsByUser(u)
cx.RespondWithData(fmt.Sprintf("%d suspended clients resumed", num))
return
}
if query.Has("suspendall") { //resume the suspended client
num := core.QMgr.SuspendAllClientsByUser(u)
cx.RespondWithData(fmt.Sprintf("%d clients suspended", num))
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例6: AuthError
func AuthError(err error, cx *goweb.Context) {
if err.Error() == e.InvalidAuth {
cx.RespondWithErrorMessage("Invalid authorization header or content", http.StatusBadRequest)
return
}
logger.Error("Error at Auth: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
示例7: Create
// POST: /job
func (cr *JobController) Create(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
// Parse uploaded form
params, files, err := ParseMultipartForm(cx.Request)
if err != nil {
if err.Error() == "request Content-Type isn't multipart/form-data" {
cx.RespondWithErrorMessage("No job file is submitted", http.StatusBadRequest)
} else {
// Some error other than request encoding. Theoretically
// could be a lost db connection between user lookup and parsing.
// Blame the user, Its probaby their fault anyway.
logger.Error("Error parsing form: " + err.Error())
cx.RespondWithError(http.StatusBadRequest)
}
return
}
_, has_upload := files["upload"]
_, has_awf := files["awf"]
if !has_upload && !has_awf {
cx.RespondWithErrorMessage("No job script or awf is submitted", http.StatusBadRequest)
return
}
//send job submission request and get back an assigned job number (jid)
var jid string
jid, err = core.QMgr.JobRegister()
if err != nil {
logger.Error("[email protected]_Create:GetNextJobNum: " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
var job *core.Job
job, err = core.CreateJobUpload(params, files, jid)
if err != nil {
logger.Error("[email protected]_Create:CreateJobUpload: " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
if token, err := request.RetrieveToken(cx.Request); err == nil {
job.SetDataToken(token)
}
core.QMgr.EnqueueTasksByJobId(job.Id, job.TaskList())
//log event about job submission (JB)
logger.Event(event.JOB_SUBMISSION, "jobid="+job.Id+";jid="+job.Jid+";name="+job.Info.Name+";project="+job.Info.Project)
cx.RespondWithData(job)
return
}
示例8: UpdateMany
// PUT: /job
func (cr *JobController) UpdateMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("resumeall") { //resume the suspended job
num := core.QMgr.ResumeSuspendedJobs()
cx.RespondWithData(fmt.Sprintf("%d suspended jobs resumed", num))
return
}
cx.RespondWithError(http.StatusNotImplemented)
}
示例9: handleAuthError
func handleAuthError(err error, cx *goweb.Context) {
switch err.Error() {
case e.MongoDocNotFound:
cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest)
return
// case e.InvalidAuth:
// cx.RespondWithErrorMessage("Invalid Authorization header", http.StatusBadRequest)
// return
}
logger.Error("Error at Auth: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
示例10: Update
// PUT: /client/{id} -> status update
func (cr *ClientController) Update(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Try to authenticate user.
u, err := request.Authenticate(cx.Request)
if err != nil && err.Error() != e.NoAuth {
cx.RespondWithErrorMessage(err.Error(), http.StatusUnauthorized)
return
}
// If no auth was provided, and anonymous read is allowed, use the public user
if u == nil {
if conf.ANON_WRITE == true {
u = &user.User{Uuid: "public"}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("subclients") { //update the number of subclients for a proxy
if count, err := strconv.Atoi(query.Value("subclients")); err != nil {
cx.RespondWithError(http.StatusNotImplemented)
} else {
core.QMgr.UpdateSubClientsByUser(id, count, u)
cx.RespondWithData("ok")
}
return
}
if query.Has("suspend") { //resume the suspended client
if err := core.QMgr.SuspendClientByUser(id, u); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client suspended")
}
return
}
if query.Has("resume") { //resume the suspended client
if err := core.QMgr.ResumeClientByUser(id, u); err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData("client resumed")
}
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例11: DeleteMany
// DELETE: /job?suspend
func (cr *JobController) DeleteMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
if query.Has("suspend") {
num := core.QMgr.DeleteSuspendedJobs()
cx.RespondWithData(fmt.Sprintf("deleted %d suspended jobs", num))
} else if query.Has("zombie") {
num := core.QMgr.DeleteZombieJobs()
cx.RespondWithData(fmt.Sprintf("deleted %d zombie jobs", num))
} else {
cx.RespondWithError(http.StatusNotImplemented)
}
return
}
示例12: AdminAuthenticated
func AdminAuthenticated(cx *goweb.Context) bool {
user, err := request.Authenticate(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
cx.RespondWithError(http.StatusUnauthorized)
} else {
request.AuthError(err, cx)
}
return false
}
if _, ok := conf.Admin_Users[user.Username]; !ok {
msg := fmt.Sprintf("user %s has no admin right", user.Username)
cx.RespondWithErrorMessage(msg, http.StatusBadRequest)
return false
}
return true
}
示例13: Create
// POST: /user
// To create a new user make a empty POST to /user with user:password
// Basic Auth encoded in the header. Return new user object.
func (cr *UserController) Create(cx *goweb.Context) {
// Log Request
LogRequest(cx.Request)
if _, ok := cx.Request.Header["Authorization"]; !ok {
cx.RespondWithError(http.StatusUnauthorized)
return
}
header := cx.Request.Header.Get("Authorization")
tmpAuthArray := strings.Split(header, " ")
authValues, err := base64.URLEncoding.DecodeString(tmpAuthArray[1])
if err != nil {
err = errors.New("Failed to decode encoded auth settings in http request.")
cx.RespondWithError(http.StatusBadRequest)
return
}
authValuesArray := strings.Split(string(authValues), ":")
if conf.ANON_CREATEUSER == false && len(authValuesArray) != 4 {
if len(authValuesArray) == 2 {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
} else {
cx.RespondWithError(http.StatusBadRequest)
return
}
}
name := authValuesArray[0]
passwd := authValuesArray[1]
admin := false
if len(authValuesArray) == 4 {
if authValuesArray[2] != fmt.Sprint(conf.SECRET_KEY) {
cx.RespondWithErrorMessage(e.UnAuth, http.StatusUnauthorized)
return
} else if authValuesArray[3] == "true" {
admin = true
}
}
u, err := user.New(name, passwd, admin)
if err != nil {
// Duplicate key check
if e.MongoDupKeyRegex.MatchString(err.Error()) {
logger.Error("[email protected]_Create: duplicate key error")
cx.RespondWithErrorMessage("Username not available", http.StatusBadRequest)
return
} else {
logger.Error("[email protected]_Create: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
cx.RespondWithData(u)
return
}
示例14: ReadMany
// GET: /logger
func (cr *LoggerController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{Li: cx.Request.URL.Query()}
// public logging info
if query.Has("event") {
cx.RespondWithData(event.EventDiscription)
return
}
if query.Has("debug") {
cx.RespondWithData(map[string]int{"debuglevel": conf.DEBUG_LEVEL})
return
}
cx.RespondWithError(http.StatusNotImplemented)
return
}
示例15: ReadMany
// GET: /user
func (cr *UserController) ReadMany(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
u, err := request.Authenticate(cx.Request)
if err != nil {
if err.Error() == e.NoAuth || err.Error() == e.UnAuth {
cx.RespondWithError(http.StatusUnauthorized)
return
} else {
logger.Error("[email protected]_Read: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
if u.Admin {
users := user.Users{}
user.AdminGet(&users)
if len(users) > 0 {
cx.RespondWithData(users)
return
} else {
cx.RespondWithNotFound()
return
}
} else {
cx.RespondWithError(http.StatusUnauthorized)
return
}
}