How to use checkErrorCode method of http Package

Best K6 code snippet using http.checkErrorCode

user.go

Source:user.go Github

copy

Full Screen

1package managers2import (3 "encoding/json"4 "net/http"5 d "restapi/dtos"6 m "restapi/models"7 u "restapi/utils"8 "strings"9 c "github.com/gorilla/context"10 "github.com/gorilla/mux"11)12// GetAllUsers users13func GetAllUsers(w http.ResponseWriter, r *http.Request) {14 if u.CheckMethodPath("GET", u.CheckRoutes["users"], w, r) {15 return16 }17 users := []m.User{}18 if u.CheckErrorCode(u.GetConnection().Preload("Organizations").Preload("Role").Find(&users).Error, w) {19 return20 }21 u.Respond(w, users)22}23// CreateUser function which receive a POST request and return a fresh-new user24func CreateUser(w http.ResponseWriter, r *http.Request) {25 if u.CheckMethodPath("POST", u.CheckRoutes["user"], w, r) {26 return27 }28 var a d.User29 json.NewDecoder(r.Body).Decode(&a)30 if a.Mail == nil || a.RoleID == 0 {31 http.Error(w, "Mail or role invalid", 400)32 return33 }34 db := u.GetConnection()35 existingUsers := []m.User{}36 db.Find(&existingUsers)37 for _, user := range existingUsers {38 if strings.EqualFold(user.Mail, *a.Mail) {39 http.Error(w, "Mail address already in use", http.StatusConflict)40 return41 }42 }43 organizations := []m.Organization{}44 role := m.Role{}45 if u.CheckErrorCode(db.Find(&organizations, a.OrganizationsID).Error, w) {46 return47 }48 if u.CheckErrorCode(db.Find(&role, a.RoleID).Error, w) {49 return50 }51 if len(organizations) != len(a.OrganizationsID) || role.ID != a.RoleID {52 http.Error(w, "invalid args", 400)53 return54 }55 user := m.User{Mail: *a.Mail, Role: &role, Organizations: organizations, IsActive: true}56 if u.CheckErrorCode(db.Preload("Organization").Preload("Role").Create(&user).Error, w) {57 return58 }59 u.RespondCreate(w, user)60}61// FindUserByID user by ID using GET Request62func FindUserByID(w http.ResponseWriter, r *http.Request) {63 if u.CheckMethodPath("GET", u.CheckRoutes["user"], w, r) {64 return65 }66 user := m.User{}67 vars := mux.Vars(r)68 if u.CheckErrorCode(u.GetConnection().Preload("Organizations").Preload("Role").Where("is_active = ?", true).First(&user, vars["id"]).Error, w) {69 return70 }71 u.Respond(w, user)72}73// DeleteUser disable user give in URL information (IsActive -> false)74func DeleteUser(w http.ResponseWriter, r *http.Request) {75 if u.CheckMethodPath("DELETE", u.CheckRoutes["user"], w, r) {76 return77 }78 user := m.User{}79 v := mux.Vars(r)80 if len(v) != 1 || !u.IsStringInt(v["id"]) {81 http.Error(w, "Bad request", 400)82 return83 }84 db := u.GetConnection()85 if u.CheckErrorCode(db.First(&user, v["id"]).Error, w) {86 return87 }88 db.Model(&user).Update("is_active", false)89}90// UpdateUser modifies a user91func UpdateUser(w http.ResponseWriter, r *http.Request) {92 if u.CheckMethodPath("PUT", u.CheckRoutes["user"], w, r) {93 return94 }95 var a d.User96 user := m.User{}97 organizations := []m.Organization{}98 role := m.Role{}99 json.NewDecoder(r.Body).Decode(&a)100 if a.ID == nil {101 http.Error(w, "bad argues", 400)102 return103 }104 contextUser := c.Get(r, "user").(*m.User)105 if contextUser.RoleID == 3 && contextUser.RoleID != a.RoleID && contextUser.ID == *a.ID { // 3 admin106 http.Error(w, "This action is not permitted on the actual user", 403)107 return108 }109 db := u.GetConnection()110 if u.CheckErrorCode(db.Preload("Role").Preload("Organizations").First(&user, *a.ID).Error, w) {111 return112 }113 if err := db.Where(a.RoleID).First(&role).Error; err != nil {114 http.Error(w, "This role does not exist", 400)115 return116 }117 db.Model(&user).Association("Role").Replace(&role)118 if a.OrganizationsID != nil {119 if u.CheckErrorCode(db.Find(&organizations, a.OrganizationsID).Error, w) {120 return121 }122 if len(organizations) != len(a.OrganizationsID) {123 http.Error(w, "bad argues", 204)124 return125 }126 db.Model(&user).Association("Organizations").Replace(organizations)127 }128 if a.Mail != nil {129 user.Mail = *a.Mail130 }131 user = m.User{ID: *a.ID, Mail: *a.Mail, Organizations: organizations, RoleID: a.RoleID, IsActive: true}132 if u.CheckErrorCode(db.Preload("Role").Preload("Organizations").Save(&user).Error, w) {133 return134 }135 u.Respond(w, user)136}...

Full Screen

Full Screen

interval.go

Source:interval.go Github

copy

Full Screen

1package managers2import (3 "encoding/json"4 "net/http"5 d "restapi/dtos"6 m "restapi/models"7 u "restapi/utils"8 c "github.com/gorilla/context"9 "github.com/gorilla/mux"10 "github.com/jinzhu/gorm"11)12// FindIntervalByID get an interval by ID13func FindIntervalByID(w http.ResponseWriter, r *http.Request) {14 if u.CheckMethodPath("GET", u.CheckRoutes["intervals"], w, r) {15 return16 }17 interval := []m.Interval{}18 vars := mux.Vars(r)19 if u.CheckErrorCode(u.GetConnection().Preload("Commentinterval").Preload("Commentinterval.User").Preload("Tags").Where("is_active = ?", true).Find(&interval, vars["id"]).Error, w) {20 return21 }22 u.Respond(w, interval)23}24// FindIntervalByAnnotationID get an interval by annotation ID25func FindIntervalByAnnotationID(w http.ResponseWriter, r *http.Request) {26 if u.CheckMethodPath("GET", u.CheckRoutes["intervalsannotations"], w, r) {27 return28 }29 interval := []m.Interval{}30 vars := mux.Vars(r)31 if u.CheckErrorCode(u.GetConnection().Preload("Commentinterval").Preload("Commentinterval.User").Preload("Tags").Where("is_active = ?", true).Where("annotation_id = ?", vars["id"]).Find(&interval).Error, w) {32 return33 }34 u.Respond(w, interval)35}36// CreateInterval create an interval37func CreateInterval(w http.ResponseWriter, r *http.Request) {38 if u.CheckMethodPath("POST", u.CheckRoutes["intervals"], w, r) {39 return40 }41 var i d.Interval42 db := u.GetConnection()43 annotation := m.Annotation{}44 contextUser := c.Get(r, "user").(*m.User)45 err := json.NewDecoder(r.Body).Decode(&i)46 if err != nil || i.TimeStart == nil || i.TimeEnd == nil || i.AnnotationID == nil && (*i.TimeStart > *i.TimeEnd) {47 http.Error(w, "Bad args", 400)48 return49 }50 if u.CheckErrorCode(db.Preload("Status").Preload("Status.EnumStatus").Preload("Status.User").Preload("Tags").First(&annotation, *i.AnnotationID).Error, w) {51 return52 }53 annotation.LastStatus, _ = annotation.GetLastAndFirstStatus()54 if annotation.LastStatus.EnumStatus.ID == 2 {55 if err := changeStatusEditDate(db, w, 3, &contextUser.ID, *i.AnnotationID); err != nil {56 if err == gorm.ErrRecordNotFound {57 http.Error(w, err.Error(), 404)58 return59 }60 if err == gorm.ErrInvalidSQL {61 http.Error(w, err.Error(), 400)62 return63 }64 http.Error(w, err.Error(), 500)65 return66 }67 }68 c := m.Interval{TimeStart: *i.TimeStart, TimeEnd: *i.TimeEnd, AnnotationID: *i.AnnotationID, IsActive: true}69 if u.CheckErrorCode(db.Create(&c).Error, w) {70 return71 }72 u.RespondCreate(w, c)73}74// RemoveIntervalByID Remove an interval by his ID75func RemoveIntervalByID(w http.ResponseWriter, r *http.Request) {76 if u.CheckMethodPath("DELETE", u.CheckRoutes["interval"], w, r) {77 return78 }79 interval := m.Interval{}80 v := mux.Vars(r)81 if len(v) != 1 || !u.IsStringInt(v["id"]) {82 http.Error(w, "Bad args", 204)83 return84 }85 db := u.GetConnection()86 if u.CheckErrorCode(db.First(&interval, v["id"]).Delete(&interval).Error, w) {87 return88 }89}90// AddTagsOnInterval create a tag on an interval91func AddTagsOnInterval(w http.ResponseWriter, r *http.Request) {92 if u.CheckMethodPath("POST", u.CheckRoutes["intervalstags"], w, r) {93 return94 }95 var i d.IntervalTagsPayload96 err := json.NewDecoder(r.Body).Decode(&i)97 if err != nil || i.Tags == nil || len(i.Tags) == 0 || i.IntervalID == nil {98 http.Error(w, "Bad request (client)", 204)99 return100 }101 tags := []m.Tag{}102 db := u.GetConnection()103 if u.CheckErrorCode(db.Find(&tags, i.Tags).Error, w) {104 return105 }106 if len(tags) != len(i.Tags) {107 http.Error(w, "Bad request (client)", 204)108 return109 }110 interval := m.Interval{}111 if u.CheckErrorCode(db.Find(&interval, *i.IntervalID).Error, w) {112 return113 }114 db.Model(&interval).Association("Tags").Replace(tags)115 u.RespondCreate(w, &tags)116}...

Full Screen

Full Screen

tag.go

Source:tag.go Github

copy

Full Screen

1package managers2import (3 "encoding/json"4 "net/http"5 d "restapi/dtos"6 m "restapi/models"7 u "restapi/utils"8 "github.com/gorilla/mux"9)10// GetAllTags list all tags11func GetAllTags(w http.ResponseWriter, r *http.Request) {12 if u.CheckMethodPath("GET", u.CheckRoutes["tags"], w, r) {13 return14 }15 tags := []m.Tag{}16 if u.CheckErrorCode(u.GetConnection().Set("gorm:auto_preload", true).Find(&tags).Error, w) {17 return18 }19 u.Respond(w, tags)20}21// CreateTag create a tag22func CreateTag(w http.ResponseWriter, r *http.Request) {23 if u.CheckMethodPath("POST", u.CheckRoutes["tag"], w, r) {24 return25 }26 var i d.Tag27 err := json.NewDecoder(r.Body).Decode(&i)28 if err != nil || i.Name == nil || i.Color == nil {29 http.Error(w, "Bad args", 204)30 return31 }32 t := m.Tag{ParentID: i.ParentID, Name: *i.Name, Color: *i.Color, IsActive: true}33 if u.CheckErrorCode(u.GetConnection().Set("gorm:auto_preload", true).Create(&t).Error, w) {34 return35 }36 u.RespondCreate(w, t)37}38// RemoveTagByID remove a tag by his id39func RemoveTagByID(w http.ResponseWriter, r *http.Request) {40 if u.CheckMethodPath("DELETE", u.CheckRoutes["tag"], w, r) {41 return42 }43 t := m.Tag{}44 v := mux.Vars(r)45 if len(v) != 1 || !u.IsStringInt(v["id"]) {46 http.Error(w, "Bad args", 204)47 return48 }49 db := u.GetConnection()50 if u.CheckErrorCode(db.First(&t, v["id"]).Error, w) {51 return52 }53 if u.CheckErrorCode(db.Model(&t).Update("is_active", false).Error, w) {54 return55 }56 u.Respond(w, &t)57}58// UpdateTagByID update a tag by his id59func UpdateTagByID(w http.ResponseWriter, r *http.Request) {60 if u.CheckMethodPath("PUT", u.CheckRoutes["tag"], w, r) {61 return62 }63 var t d.Tag64 err := json.NewDecoder(r.Body).Decode(&t)65 if err != nil || (t.Color == nil || t.Name == nil || t.ID == nil) {66 http.Error(w, "Bad args", 400)67 return68 }69 db := u.GetConnection()70 tag := m.Tag{}71 if u.CheckErrorCode(db.First(&tag, *t.ID).Error, w) {72 return73 }74 if t.Color != nil {75 tag.Color = *t.Color76 }77 if t.Name != nil {78 tag.Name = *t.Name79 }80 if t.ParentID != nil {81 tag.ParentID = t.ParentID82 }83 if t.IsActive != nil {84 tag.IsActive = *t.IsActive85 }86 if u.CheckErrorCode(db.Save(&tag).Error, w) {87 return88 }89 u.Respond(w, &tag)90}...

Full Screen

Full Screen

checkErrorCode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println("Error:", err)5 }6 defer resp.Body.Close()7 if resp.StatusCode != 200 {8 fmt.Println("Status code error:", resp.StatusCode, http.StatusText(resp.StatusCode))9 }10 fmt.Println("Success!")11}12import (13func main() {14 if err != nil {15 fmt.Println("Error:", err)16 }17 defer resp.Body.Close()18 if resp.StatusCode != http.StatusOK {19 fmt.Println("Status code error:", resp.StatusCode, http.StatusText(resp.StatusCode))20 }21 fmt.Println("Success!")22}23import (24func main() {25 if err != nil {26 fmt.Println("Error:", err)27 }28 defer resp.Body.Close()29 if resp.StatusCode != http.StatusOK {30 fmt.Println("Status code error:", resp.StatusCode, http.StatusText(resp.StatusCode))31 }32 fmt.Println("Success!")33}34import (35func main() {36 if err != nil {37 fmt.Println("Error:", err)38 }39 defer resp.Body.Close()40 if resp.StatusCode != http.StatusOK {41 fmt.Println("Status code error:", resp.StatusCode, http.StatusText(resp.StatusCode))42 }43 fmt.Println("Success!")44}45import (46func main() {47 if err != nil {48 fmt.Println("Error:", err)49 }50 defer resp.Body.Close()51 if resp.StatusCode != http.StatusOK {52 fmt.Println("Status code error:", resp.StatusCode, http.StatusText

Full Screen

Full Screen

checkErrorCode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(http.StatusText(200))4 fmt.Println(http.StatusText(300))5 fmt.Println(http.StatusText(400))6 fmt.Println(http.StatusText(500))7}

Full Screen

Full Screen

checkErrorCode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(http.StatusText(500))4 fmt.Println(http.StatusText(404))5 fmt.Println(http.StatusText(200))6}

Full Screen

Full Screen

checkErrorCode

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(resp.StatusCode)4 fmt.Println(resp.Status)5}6import (7func main() {8 fmt.Println(resp.StatusCode)9 fmt.Println(resp.Status)10}11import (12func main() {13 fmt.Println(resp.StatusCode)14 fmt.Println(resp.Status)15}16import (17func main() {18 fmt.Println(resp.StatusCode)19 fmt.Println(resp.Status)20}21import (22func main() {23 fmt.Println(resp.StatusCode)24 fmt.Println(resp.Status)25}26import (27func main() {28 fmt.Println(resp.StatusCode)29 fmt.Println(resp.Status)30}31import (32func main() {33 fmt.Println(resp.StatusCode)34 fmt.Println(resp.Status)35}36import (37func main() {38 fmt.Println(resp.StatusCode)39 fmt.Println(resp.Status)40}41import (42func main() {43 fmt.Println(resp.StatusCode)

Full Screen

Full Screen

checkErrorCode

Using AI Code Generation

copy

Full Screen

1import "net/http"2import "fmt"3func main() {4 fmt.Println(http.StatusText(200))5 fmt.Println(http.StatusText(400))6 fmt.Println(http.StatusText(500))7}8import "fmt"9import "net/http"10func main() {11 fmt.Println(http.StatusText(200))12 fmt.Println(http.StatusText(400))13 fmt.Println(http.StatusText(500))14}15import "fmt"16import "net/http"17func main() {18 fmt.Println(http.StatusText(200))19 fmt.Println(http.StatusText(400))20 fmt.Println(http.StatusText(500))21}22import "fmt"23import "net/http"24func main() {25 fmt.Println(http.StatusText(200))26 fmt.Println(http.StatusText(400))27 fmt.Println(http.StatusText(500))28}29import "fmt"30import "net/http"31func main() {32 fmt.Println(http.StatusText(200))33 fmt.Println(http.StatusText(400))34 fmt.Println(http.StatusText(500))35}36import "fmt"37import "net/http"38func main() {39 fmt.Println(http.StatusText(200))40 fmt.Println(http.StatusText(400))41 fmt.Println(http.StatusText(500))42}43import "fmt"44import "net/http"45func main() {46 fmt.Println(http.StatusText(200))47 fmt.Println(http.StatusText(400))48 fmt.Println(http.StatusText(500))49}50import "fmt"51import "net/http"52func main() {53 fmt.Println(http.StatusText(200))54 fmt.Println(http.StatusText(400))55 fmt.Println(http.StatusText(500))56}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful