improve logging (#164)

Co-authored-by: AlteredCoder <AlteredCoder>
This commit is contained in:
AlteredCoder 2020-07-30 15:30:02 +02:00 committed by GitHub
parent f8abb01bbc
commit d23512e9c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 13 deletions

View file

@ -141,17 +141,18 @@ func (ctx *ApiCtx) Signin() error {
return fmt.Errorf("api signin: missing credentials in api.yaml")
}
jsonResp := &ApiResp{}
errResp := &ApiResp{}
resp, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api signin: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api signin failed. http response")
return fmt.Errorf("api signin failed")
}
ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
@ -164,14 +165,15 @@ func (ctx *ApiCtx) RegisterMachine(machineID string, password string) error {
ctx.Creds.User = machineID
ctx.Creds.Password = password
jsonResp := &ApiResp{}
errResp := &ApiResp{}
resp, err := ctx.Http.New().Post(ctx.RegisterPath).BodyJSON(ctx.Creds).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.RegisterPath).BodyJSON(ctx.Creds).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api register machine: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api register machine: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api register machine: return bad HTTP code (%d) : %s", resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
@ -184,15 +186,16 @@ func (ctx *ApiCtx) ResetPassword(machineID string, password string) error {
ctx.Creds.User = machineID
ctx.Creds.Password = password
jsonResp := &ApiResp{}
errResp := &ApiResp{}
data := map[string]string{"machine_id": ctx.Creds.User, "password": ctx.Creds.Password}
resp, err := ctx.Http.New().Post(ctx.ResetPwdPath).BodyJSON(data).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.ResetPwdPath).BodyJSON(data).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api reset password: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api reset password: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api reset password: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.StatusCode != 200 {

View file

@ -9,14 +9,15 @@ import (
func (ctx *ApiCtx) Enroll(userID string) error {
toPush := map[string]string{"user_id": userID}
jsonResp := &ApiResp{}
errResp := &ApiResp{}
resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api enroll: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d)", userID, resp.StatusCode)
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d): %s", userID, resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api user enroll failed")

View file

@ -8,13 +8,15 @@ import (
func (ctx *ApiCtx) PullTop() ([]map[string]string, error) {
top := &PullResp{}
resp, err := ctx.Http.New().Get(ctx.PullPath).ReceiveSuccess(top)
errResp := &ApiResp{}
resp, err := ctx.Http.New().Get(ctx.PullPath).Receive(top, errResp)
if err != nil {
return nil, fmt.Errorf("api pull: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("api pull: return bad HTTP code (%d)", resp.StatusCode)
return nil, fmt.Errorf("api pull: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
log.Debugf("api pull: response : %+v", top.Body)

View file

@ -22,7 +22,8 @@ func (ctx *ApiCtx) pushSignals() error {
return nil
}
jsonResp := &ApiResp{}
resp, err := ctx.Http.New().Put(ctx.PushPath).BodyJSON(&ctx.toPush).ReceiveSuccess(jsonResp)
errResp := &ApiResp{}
resp, err := ctx.Http.New().Put(ctx.PushPath).BodyJSON(&ctx.toPush).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api push signal: HTTP request creation failed: %s", err)
}
@ -41,7 +42,7 @@ func (ctx *ApiCtx) pushSignals() error {
return fmt.Errorf("api push signal: unable to renew api session token: %s", err.Error())
}
} else {
return fmt.Errorf("api push signal: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api push signal: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
}