api

package
v0.0.0-...-0d0a0fb Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: GPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogAndRespondAppError

func LogAndRespondAppError(w http.ResponseWriter, ctx Context, appError *AppError)

LogAndRespondAppError - log and respond an app error

func LogAppError

func LogAppError(logger logrus.FieldLogger, appErr *AppError)

LogAppError - Log the error

func RespondAppError

func RespondAppError(w http.ResponseWriter, render *render.Render, appError *AppError)

RespondAppError - standard method for returning JSON error responses from application

Types

type AppError

type AppError struct {
	ID       string
	Message  string
	Cause    string
	Type     ErrorTypeEnum
	Severity ErrorSeverityEnum
}

AppError - hold all the information regarding an error for an application

func CreateDatabaseConnectionAppError

func CreateDatabaseConnectionAppError(err error, severityLevel ErrorSeverityEnum) *AppError

CreateDatabaseConnectionAppError - creates a database connection error (503 status code)

func CreateInvalidRequestAppError

func CreateInvalidRequestAppError(err error, severityLevel ErrorSeverityEnum) *AppError

CreateInvalidRequestAppError - creates a bad request error (400 status code)

func CreateInvalidltemAppError

func CreateInvalidltemAppError(item string, err error, severityLevel ErrorSeverityEnum) *AppError

CreateInvalidltemAppError - for a specified item and error creates a validation error with an error message stating item is invalid (400 status code)

func CreateInvalidltemCauseAppError

func CreateInvalidltemCauseAppError(item, cause string, severityLevel ErrorSeverityEnum) *AppError

CreateInvalidltemCauseAppError - for a specified item and cause creates a validation error With an error message stating item is invalid (400 status code)

func CreateNotAuthorisedAppError

func CreateNotAuthorisedAppError(data string) *AppError

CreateNotAuthorisedAppError - creates a method not allowed error (403 status code)

func CreateNotFoundInCollectionAppError

func CreateNotFoundInCollectionAppError(id, collection string, err error, severityLevel ErrorSeverityEnum) *AppError

CreateNotFoundInCollectionAppError - creates a not found error with an error message stating id is not found in specified collection (404 status code)

func CreateNotFoundItemAppError

func CreateNotFoundItemAppError(item, id string, err error, severityLevel ErrorSeverityEnum) *AppError

CreateNotFoundItemAppError - creates a not found error With an error message stating item with that id is not found (404 status code)

func CreateValidationAppError

func CreateValidationAppError(err error, severityLevel ErrorSeverityEnum) *AppError

CreateValidationAppError - creates a validation error with an error message stating which fields are invalid (400 status code)

func NewAppError

func NewAppError(message, cause string, errorType ErrorTypeEnum, errorSeverity ErrorSeverityEnum) *AppError

NewAppError - creates a new instance of an application error

func (*AppError) Error

func (ae *AppError) Error() string

func (*AppError) GetHTTPStatusCode

func (ae *AppError) GetHTTPStatusCode() int

GetHTTPStatusCode - returns the http status code applicable for the type of error

func (*AppError) GetUserError

func (ae *AppError) GetUserError() *UserError

GetUserError return information just for the user

type Config

type Config struct {
	Port        int    `short:"p" long:"port" env:"PORT" default:"8080"`
	Hostname    string `long:"hostname" description:"Hostname for wherever we're running" env:"HOSTNAME"`
	LogLevel    string `long:"loglevel" env:"LOG_LEVEL" default:"DEBUG"`
	LogFormat   string `long:"logformat" env:"LOG_FORMAT" default:"TEXT"`
	PrettyPrint bool   `long:"pretty" env:"PRETTY"`

	RateLimitMaxRequestsCapacity int64 `` /* 141-byte string literal not displayed */
	RateLimitRefillRatePerSecond int   `` /* 155-byte string literal not displayed */

	DBServer   string `long:"dbserver" env:"DB_SERVER" default:"localhost:27017"`
	DBName     string `long:"dbname" env:"DB_NAME"`
	DBUser     string `long:"dbuser" env:"DB_USER"`
	DBPassword string `long:"dbpassword" env:"DB_PASSWORD"`

	MessagingURL   string `long:"msurl" env:"MS_URL"`
	MessagingTopic string `long:"mstopic" env:"MS_TOPIC"`

	TLS      bool   `long:"tls" env:"TLS"`
	CertFile string `long:"certfile" env:"CERT_FILE"`
	KeyFile  string `long:"keyfile" env:"KEY_FILE"`
}

Config contains config read from environment variables

type Context

type Context interface {
	Log() logrus.FieldLogger
	BaseLogger() *logrus.Logger
	GenericLogger(string, string, interface{}) *log.Logger
	Render() *render.Render
	Validate() *validator.Validate
	Wrap(fields map[string]interface{}) Context
}

Context interface for context so it can be mocked for testing

func NewContext

func NewContext(config *Config) Context

NewContext creates a new instance of application context from parsing environment conf ig

type ErrorSeverityEnum

type ErrorSeverityEnum uint

ErrorSeverityEnum - enum for the severity level of application error

const (
	//EnumSeverityLow - Something noteworthy happened!
	EnumSeverityLow ErrorSeverityEnum = iota
	//EnumSeverityMedium - You should probably take a look at this
	EnumSeverityMedium
	//EnumSeverityHigh - Something failed but I'm not quitting
	EnumSeverityHigh
	//EnumSeverityFatal - Bye
	EnumSeverityFatal
)

func (ErrorSeverityEnum) String

func (severity ErrorSeverityEnum) String() string

type ErrorTypeEnum

type ErrorTypeEnum string

ErrorTypeEnum - enum for the type of application error, more will be added

const (
	// EnumTypeValidation - for invalid input from the user
	EnumTypeValidation ErrorTypeEnum = "Validation"
	// EnumTypeExistence - to represent an item not existing
	EnumTypeExistence ErrorTypeEnum = "Existence"
	// EnumTypeAuthentication - to represent a user or networked traffic is not authorised and the operation should not be repeated
	EnumTypeAuthentication ErrorTypeEnum = "Authentication"
	// EnumTypeAuthorisation - to represent a user not authorised due to missing or invalid authentication
	EnumTypeAuthorisation ErrorTypeEnum = "Authorisation"
	// EnumTypeConnection - to show that the system could not connect to an external service
	EnumTypeConnection ErrorTypeEnum = "Connection"
	// EnumTypeOverload - error type to show that too much work has been requested
	EnumTypeOverload ErrorTypeEnum = "Overload"
	// EnumTypeUnrecognised - error type to show that the error type cannot be determined
	EnumTypeUnrecognised ErrorTypeEnum = "Unrecognised"
)

func (ErrorTypeEnum) String

func (errorType ErrorTypeEnum) String() string

type UserError

type UserError struct {
	ID       string `json:"id"`
	Message  string `json:"message"`
	Cause    string `json:"cause"`
	Severity string `json:"severity"`
}

UserError structure holding just information for the user

Jump to

Keyboard shortcuts

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