tracerlogger

package module
v0.0.0-...-427675c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

Tracer Logger

Go library for tracing and logging.

Logger package

A simple logging wrapper around Uber's Zap.

Tracer package

A tracing middleware for HTTP requests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomHex

func RandomHex(n int) (string, error)

RandomHex generates a random hex value.

func RespondWithError

func RespondWithError(w http.ResponseWriter, code int, err error)

RespondWithJSON send a JSON-formatted error response.

func RespondWithJSON

func RespondWithJSON(w http.ResponseWriter, code int, payload interface{})

RespondWithJSON send a JSON-formatted response, including the HSTS policy header.

Types

type CodeError

type CodeError string

CodeError represents custom error codes for HTTP error responses.

const (

	// CodeBadRequest - CodeError BadRequest
	CodeBadRequest CodeError = "400"
	// CodeUnauthorized - CodeError Unauthorized
	CodeUnauthorized CodeError = "401"
	// CodeForbidden - CodeError Forbidden
	CodeForbidden CodeError = "403"
	// CodeNotFound - CodeError NotFound
	CodeNotFound CodeError = "404"
	// CodeInternalServerError - CodeError InternalServerError
	CodeInternalServerError CodeError = "500"

	// CodeFieldsValidation - CodeError FieldsValidation
	CodeFieldsValidation CodeError = "10000"
	// CodeUniqueFieldValidation - CodeError UniqueFieldValidation
	CodeUniqueFieldValidation CodeError = "10001"
	// CodeFieldMaxLength - CodeError FieldMaxLength
	CodeFieldMaxLength CodeError = "10002"
	// CodeFieldRequired - CodeError FieldRequired
	CodeFieldRequired CodeError = "10003"
	// CodeRouteVariableRequired - CodeError RouteVariableRequired
	CodeRouteVariableRequired CodeError = "10004"
	// CodeFieldMinValue - CodeError FieldMinValue
	CodeFieldMinValue CodeError = "10005"
	// CodeFieldInvalidValue - CodeError FieldInvalidValue
	CodeFieldInvalidValue CodeError = "10006"
	// CodeRequestPayloadMalformed - CodeError RequestPayloadMalformed
	CodeRequestPayloadMalformed CodeError = "10007"
	// CodeFieldNotMatchRegex - CodeError FieldNotMatchRegex
	CodeFieldNotMatchRegex CodeError = "10008"
	// CodeRequestTokenMalformed - CodeError RequestTokenMalformed
	CodeRequestTokenMalformed CodeError = "10009"
	// CodeExpiredRequestToken - CodeError ExpiredRequestToken
	CodeExpiredRequestToken CodeError = "10010"
)

func (CodeError) CodeError

func (ce CodeError) CodeError() CodeError

CodeError returns itself.

func (CodeError) Error

func (ce CodeError) Error() string

Error returns the error message corresponding to the CodeError.

func (CodeError) Respond

func (ce CodeError) Respond(w http.ResponseWriter, code int, err error)

Respond sends an HTTP error response corresponding to the CodeError.

func (CodeError) ResponseError

func (ce CodeError) ResponseError() (ResponseError, bool)

ResponseError returns the corresponding ResponseError for the CodeError. If the CodeError is not found, it defaults to CodeInternalServerError.

func (CodeError) String

func (ce CodeError) String() string

String returns a formatted string representation of the CodeError.

type Error

type Error interface {
	CodeError() CodeError
	Error() string
	Respond(w http.ResponseWriter, code int, err error)
}

Error interface for HTTP error responses.

type FieldError

type FieldError struct {
	Code    string `json:"code"`
	Field   string `json:"field"`
	Message string `json:"message,omitempty"`
}

FieldError represents an error associated with a specific field.

func (FieldError) Error

func (fe FieldError) Error() string

Error returns the error message corresponding to the FieldError.

func (FieldError) String

func (fe FieldError) String() string

String returns a formatted string representation of the FieldError.

type ResponseError

type ResponseError struct {
	Code    string       `json:"code,omitempty"`
	Title   string       `json:"title,omitempty"`
	Message string       `json:"message,omitempty"`
	Errors  []FieldError `json:"errors,omitempty"`
}

ResponseError represents a structured error response.

func (*ResponseError) AddValidationError

func (re *ResponseError) AddValidationError(code CodeError, field, message string)

AddValidationError appends a FieldError to ResponseError's Errors slice.

func (ResponseError) CodeError

func (re ResponseError) CodeError() CodeError

CodeError returns the code associated with the ResponseError.

func (ResponseError) Error

func (re ResponseError) Error() string

Error returns the error message corresponding to the ResponseError.

func (ResponseError) Respond

func (re ResponseError) Respond(w http.ResponseWriter, code int, err error)

Respond sends an HTTP error response using the ResponseError structure.

func (ResponseError) String

func (re ResponseError) String() string

String returns a formatted string representation of the ResponseError.

Directories

Path Synopsis
This package is a reduced version of the go trace implementations
This package is a reduced version of the go trace implementations
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL