Skip to content

Commit

Permalink
[ISSUE-35] add stack trace to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Mar 21, 2024
1 parent a7b7879 commit 16593ab
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 98 deletions.
56 changes: 23 additions & 33 deletions assert.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
package cute

import (
"fmt"
"net/http"
"runtime"
)

// This is type of asserts, for create some assert with using custom logic.

// AssertBody ...
// AssertBody is type for create custom assertions for body
// Example asserts:
// - json.LengthGreaterThan
// - json.LengthGreaterOrEqualThan
// - json.LengthLessThan
// - json.LengthLessOrEqualThan
// - json.Present
// - json.NotEmpty
// - json.NotPresent
type AssertBody func(body []byte) error

// AssertHeaders ...
// AssertHeaders is type for create custom assertions for headers
// Example asserts:
// - headers.Present
// - headers.NotPresent
type AssertHeaders func(headers http.Header) error

// AssertResponse ...
// AssertResponse is type for create custom assertions for response
type AssertResponse func(response *http.Response) error

// This is type for create custom assertions with using allure and testing.allureProvider

// AssertBodyT ...
// AssertBodyT is type for create custom assertions for body with TB
// Check example in AssertBody
// TB is testing.T and it can be used for require ore assert from testify or another packages
type AssertBodyT func(t T, body []byte) error

// AssertHeadersT ...
// AssertHeadersT is type for create custom assertions for headers with TB
// Check example in AssertHeaders
// TB is testing.T and it can be used for require ore assert from testify or another packages
type AssertHeadersT func(t T, headers http.Header) error

// AssertResponseT ...
// AssertResponseT is type for create custom assertions for response with TB
// Check example in AssertResponse
// TB is testing.T and it can be used for require ore assert from testify or another packages
type AssertResponseT func(t T, response *http.Response) error

func callAssertWrapper(assert AssertBody, withCall string) func(body []byte) error {
return func(body []byte) error {
err := assert(body)

if err != nil {
return fmt.Errorf("%w\n%s", err, withCall)
}

return nil
}
}

func getCaller() string {
pcs := make([]uintptr, 10)
depth := runtime.Callers(3, pcs)
if depth == 0 {
fmt.Println("Couldn't get the stack information")
return ""
}
callers := runtime.CallersFrames(pcs[:depth])
caller, _ := callers.Next()

return fmt.Sprintf("Called from: %s:%d\n", caller.File, caller.Line)
}

func (it *Test) assertHeaders(t internalT, headers http.Header) []error {
var (
asserts = it.Expect.AssertHeaders
Expand Down
14 changes: 0 additions & 14 deletions assert_broken.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ func brokenAssertResponseT(assert AssertResponseT) AssertResponseT {
}
}

func wrapWithTrace(err error, trace string) error {
if err == nil {
return nil
}

if tErr, ok := err.(errors.WithTrace); ok {
tErr.SetTrace(trace)

return tErr.(error)
}

return errors.WrapErrorWithTrace(err, trace)
}

func wrapBrokenError(err error) error {
if err == nil {
return nil
Expand Down
91 changes: 91 additions & 0 deletions assert_trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cute

import (
"fmt"
"net/http"
"runtime"

"github.com/ozontech/cute/errors"
)

// assertHeadersWithTrace is a function to add trace inside assert headers error
func assertHeadersWithTrace(assert AssertHeaders, trace string) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)

return wrapWithTrace(err, trace)
}
}

// assertBodyWithTrace is a function to add trace inside assert body error
func assertBodyWithTrace(assert AssertBody, trace string) AssertBody {
return func(body []byte) error {
err := assert(body)

return wrapWithTrace(err, trace)
}
}

// assertResponseWithTrace is a function to add trace inside assert response error
func assertResponseWithTrace(assert AssertResponse, trace string) AssertResponse {
return func(resp *http.Response) error {
err := assert(resp)

return wrapWithTrace(err, trace)
}
}

// assertHeadersTWithTrace is a function to add trace inside assert headers error
func assertHeadersTWithTrace(assert AssertHeadersT, trace string) AssertHeadersT {
return func(t T, headers http.Header) error {
err := assert(t, headers)

return wrapWithTrace(err, trace)
}
}

// assertBodyTWithTrace is a function to add trace inside assert body error
func assertBodyTWithTrace(assert AssertBodyT, trace string) AssertBodyT {
return func(t T, body []byte) error {
err := assert(t, body)

return wrapWithTrace(err, trace)
}
}

// assertResponseTWithTrace is a function to add trace inside assert response error
func assertResponseTWithTrace(assert AssertResponseT, trace string) AssertResponseT {
return func(t T, resp *http.Response) error {
err := assert(t, resp)

return wrapWithTrace(err, trace)
}
}

// wrapWithTrace is a function to add trace inside error
func wrapWithTrace(err error, trace string) error {
if err == nil {
return nil
}

if tErr, ok := err.(errors.WithTrace); ok {
tErr.SetTrace(trace)

return tErr.(error)
}

return errors.WrapErrorWithTrace(err, trace)
}

func getTrace() string {
pcs := make([]uintptr, 10)
depth := runtime.Callers(3, pcs)
if depth == 0 {
fmt.Println("Couldn't get the stack information")
return ""
}
callers := runtime.CallersFrames(pcs[:depth])
caller, _ := callers.Next()

return fmt.Sprintf("%s:%d", caller.File, caller.Line)
}

0 comments on commit 16593ab

Please sign in to comment.