本文整理汇总了Golang中github.com/jaredwilkening/goweb.Context.RespondWithErrorMessage方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.RespondWithErrorMessage方法的具体用法?Golang Context.RespondWithErrorMessage怎么用?Golang Context.RespondWithErrorMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jaredwilkening/goweb.Context
的用法示例。
在下文中一共展示了Context.RespondWithErrorMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Read
// GET: /client/{id}
func (cr *ClientController) Read(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Gather query params
query := &Query{list: cx.Request.URL.Query()}
if query.Has("heartbeat") {
client, err := queueMgr.ClientHeartBeat(id)
if err != nil {
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
} else {
cx.RespondWithData(client.Id)
}
return
}
client, err := queueMgr.GetClient(id)
if err != nil {
if err.Error() == e.ClientNotFound {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
} else {
Log.Error("Error in GET client:" + err.Error())
cx.RespondWithError(http.StatusBadRequest)
}
return
}
cx.RespondWithData(client)
}
示例2: Create
// POST: /client
func (cr *ClientController) Create(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
// Parse uploaded form
_, files, err := ParseMultipartForm(cx.Request)
if err != nil {
if err.Error() != "request Content-Type isn't multipart/form-data" {
Log.Error("Error parsing form: " + err.Error())
cx.RespondWithError(http.StatusBadRequest)
return
}
}
client, err := queueMgr.RegisterNewClient(files)
if err != nil {
msg := "Error in registering new client:" + err.Error()
Log.Error(msg)
cx.RespondWithErrorMessage(msg, http.StatusBadRequest)
return
}
//log event about client registration (CR)
Log.Event(EVENT_CLIENT_REGISTRATION, "clientid="+client.Id+";name="+client.Name)
cx.RespondWithData(client)
return
}
示例3: Create
// POST: /node
func (cr *NodeController) Create(cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
u, err := AuthenticateRequest(cx.Request)
if err != nil && err.Error() != e.NoAuth {
handleAuthError(err, cx)
return
}
// Fake public user
if u == nil {
if conf.ANON_WRITE {
u = &user.User{Uuid: ""}
} else {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
}
// Parse uploaded form
params, files, err := ParseMultipartForm(cx.Request)
if err != nil {
// If not multipart/form-data it will create an empty node.
// TODO: create another request parser for non-multipart request
// to handle this cleaner.
if err.Error() == "request Content-Type isn't multipart/form-data" {
node, err := store.CreateNodeUpload(u, params, files)
if err != nil {
log.Error("Error at create empty: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
if node == nil {
// Not sure how you could get an empty node with no error
// Assume it's the user's fault
cx.RespondWithError(http.StatusBadRequest)
return
} else {
cx.RespondWithData(node)
return
}
} 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.
log.Error("Error parsing form: " + err.Error())
cx.RespondWithError(http.StatusBadRequest)
return
}
}
// Create node
node, err := store.CreateNodeUpload(u, params, files)
if err != nil {
log.Error("err " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
cx.RespondWithData(node)
return
}
示例4: Delete
// DELETE: /job/{id}
func (cr *JobController) Delete(id string, cx *goweb.Context) {
LogRequest(cx.Request)
if err := queueMgr.DeleteJob(id); err != nil {
cx.RespondWithErrorMessage("fail to delete job: "+id, http.StatusBadRequest)
return
}
cx.RespondWithData("job deleted: " + id)
return
}
示例5: 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
}
log.Error("Error at Auth: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
示例6: Update
// PUT: /client/{id} -> status update
func (cr *ClientController) Update(id string, cx *goweb.Context) {
LogRequest(cx.Request)
client, err := queueMgr.ClientHeartBeat(id)
if err != nil {
if err.Error() == e.ClientNotFound {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
} else {
Log.Error("Error in handle client heartbeat:" + err.Error())
cx.RespondWithError(http.StatusBadRequest)
}
return
}
cx.RespondWithData(client)
}
示例7: Read
// GET: /work/{id}
// get a workunit by id, read-only
func (cr *WorkController) Read(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Load workunit by id
workunit, err := queueMgr.GetWorkById(id)
if err != nil {
if err.Error() != e.QueueEmpty {
Log.Error("[email protected]_Read:QueueMgr.GetWorkById(): " + err.Error())
}
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
// Base case respond with workunit in json
cx.RespondWithData(workunit)
return
}
示例8: ReadMany
// GET: /work
// checkout a workunit with earliest submission time
// to-do: to support more options for workunit checkout
func (cr *WorkController) ReadMany(cx *goweb.Context) {
// Gather query params
query := &Query{list: cx.Request.URL.Query()}
if !query.Has("client") { //view workunits
var workunits []*core.Workunit
if query.Has("state") {
workunits = queueMgr.ShowWorkunits(query.Value("state"))
} else {
workunits = queueMgr.ShowWorkunits("")
}
cx.RespondWithData(workunits)
return
}
//checkout a workunit in FCFS order
clientid := query.Value("client")
workunits, err := queueMgr.CheckoutWorkunits("FCFS", clientid, 1)
if err != nil {
if err.Error() != e.QueueEmpty && err.Error() != e.NoEligibleWorkunitFound {
Log.Error("[email protected]_ReadMany:QueueMgr.GetWorkByFCFS(): " + err.Error())
}
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
//log access info only when the queue is not empty, save some log
LogRequest(cx.Request)
//log event about workunit checkout (WO)
workids := []string{}
for _, work := range workunits {
workids = append(workids, work.Id)
}
Log.Event(EVENT_WORK_CHECKOUT,
"workids="+strings.Join(workids, ","),
"clientid="+clientid)
// Base case respond with node in json
cx.RespondWithData(workunits[0])
return
}
示例9: 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.
Log.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 = queueMgr.JobRegister()
if err != nil {
Log.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 {
Log.Error("[email protected]_Create:CreateJobUpload: " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
queueMgr.EnqueueTasksByJobId(job.Id, job.TaskList())
//log event about job submission (JB)
Log.Event(EVENT_JOB_SUBMISSION, "jobid="+job.Id+";jid="+job.Jid+";name="+job.Info.Name+";project="+job.Info.Project)
cx.RespondWithData(job)
return
}
示例10: Read
// GET: /job/{id}
func (cr *JobController) Read(id string, cx *goweb.Context) {
LogRequest(cx.Request)
// Load job by id
job, err := core.LoadJob(id)
if err != nil {
if err.Error() == e.MongoDocNotFound {
cx.RespondWithNotFound()
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
Log.Error("[email protected]_Read:LoadJob: " + id + ":" + err.Error())
cx.RespondWithErrorMessage("job not found:"+id, http.StatusBadRequest)
return
}
}
// Base case respond with job in json
cx.RespondWithData(job)
return
}
示例11: ReadMany
// GET: /client
func (cr *ClientController) ReadMany(cx *goweb.Context) {
LogRequest(cx.Request)
clients := queueMgr.GetAllClients()
if len(clients) == 0 {
cx.RespondWithErrorMessage(e.ClientNotFound, http.StatusBadRequest)
return
}
query := &Query{list: cx.Request.URL.Query()}
filtered := []*core.Client{}
if query.Has("busy") {
for _, client := range clients {
if len(client.Current_work) > 0 {
filtered = append(filtered, client)
}
}
} else {
filtered = clients
}
cx.RespondWithData(filtered)
}
示例12: streamDownload
func streamDownload(cx *goweb.Context, node *store.Node, filename string) {
query := &Query{list: cx.Request.URL.Query()}
nf, err := node.FileReader()
if err != nil {
// File not found or some sort of file read error.
// Probably deserves more checking
log.Error("err:@preAuth node.FileReader: " + err.Error())
cx.RespondWithError(http.StatusBadRequest)
return
}
if query.Has("filename") {
filename = query.Value("filename")
}
s := &streamer{rs: []store.SectionReader{nf}, ws: cx.ResponseWriter, contentType: "application/octet-stream", filename: filename, size: node.File.Size, filter: nil}
err = s.stream()
if err != nil {
// causes "multiple response.WriteHeader calls" error but better than no response
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
log.Error("err:@preAuth: s.stream: " + err.Error())
}
}
示例13: Update
// PUT: /work/{id} -> status update
func (cr *WorkController) Update(id string, cx *goweb.Context) {
// Log Request and check for Auth
LogRequest(cx.Request)
// Gather query params
query := &Query{list: cx.Request.URL.Query()}
if query.Has("status") && query.Has("client") { //notify execution result: "done" or "fail"
notice := core.Notice{WorkId: id, Status: query.Value("status"), ClientId: query.Value("client")}
defer queueMgr.NotifyWorkStatus(notice)
if query.Has("report") { // if "report" is specified in query, parse performance statistics
_, files, err := ParseMultipartForm(cx.Request)
if err != nil {
Log.Error("[email protected]_Update_ParseMultipartForm: " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
if _, ok := files["perf"]; !ok {
Log.Error("[email protected]_Update: no perf file uploaded")
cx.RespondWithErrorMessage("no perf file uploaded", http.StatusBadRequest)
return
}
if err := queueMgr.FinalizeWorkPerf(id, files["perf"].Path); err != nil {
Log.Error("[email protected]_Update_FinalizeWorkPerf: " + err.Error())
cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest)
return
}
}
}
cx.RespondWithData("ok")
return
}
示例14: Delete
// DELETE: /node/{id}
func (cr *NodeController) Delete(id string, cx *goweb.Context) {
LogRequest(cx.Request)
u, err := AuthenticateRequest(cx.Request)
if err != nil && err.Error() != e.NoAuth {
handleAuthError(err, cx)
return
}
if u == nil {
cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
return
}
// Load node and handle user unauthorized
node, err := store.LoadNode(id, u.Uuid)
if err != nil {
if err.Error() == e.UnAuth {
cx.RespondWithError(http.StatusUnauthorized)
return
} else if err.Error() == e.MongoDocNotFound {
cx.RespondWithNotFound()
return
} else {
// In theory the db connection could be lost between
// checking user and load but seems unlikely.
log.Error("[email protected]_Read:Delete: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
if err := node.Delete(); err == nil {
cx.RespondWithOK()
return
} else {
log.Error("[email protected]_Delet:Delete: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
}
return
}
示例15: 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()) {
log.Error("[email protected]_Create: duplicate key error")
cx.RespondWithErrorMessage("Username not available", http.StatusBadRequest)
return
} else {
log.Error("[email protected]_Create: " + err.Error())
cx.RespondWithError(http.StatusInternalServerError)
return
}
}
cx.RespondWithData(u)
return
}