commonserrors

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

README

Commons Errors

Go Reference

This module is part of FINACORE software and has as objective to facilitate the error identification and handling throughout the time of software execution.

As all other products and modules of FINACORE project, this package has a strong code documentations that generates the fullfil tutorial disponible at go.dev reference page, that is enough for their usage.

Read the full documentation at: https://pkg.go.dev/github.com/finacore/commons-errors

Software Quality

Quality Gate Status Maintainability Rating Security Rating Coverage Vulnerabilities

This lib use Sonarcloud combined with Horusec to understand the code quality and security, blocking the pull request in any vulnerability incidence.

As well as security, performance is a fundamental factor of all FINACORE software, package or modules. This way this package use the benchmark regression to ensure that no performance gaps ocours.

The result of benchmark regression is disponible at: Performance Regeression

License

Copyright 2022 finacore.net

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package commonserrors encapsulates some methods and data structure responsibles to organize and manage the erros obtained duruing the program execution.

Among the characteristics, the one that stand out are the capability to convert the data structure to an string, following the error interface implemented by the golang error, As well the agregation of an integer error code that simplify the error recognitions less the necessity of make reflections in the received object.

The first step to use this package is to get it through the command below, then you will be able to import it in your code.

go get github.com/finacore/commons-errors

To avoid the conflicts between packages during the imnport phase, is strongly recommended select other name for this package in the moment of import like shown below:

import (
	commonserrors "github.com/finacore/commons-errors"
)

Once imported you can use the package as you prefer. So read the function and methods documentation to know how to use this packagage.

Package commonserrors encapsulates some methods and data structure responsibles to organize and manage the erros obtained duruing the program execution.

Among the characteristics, the one that stand out are the capability to convert the data structure to an string, following the error interface implemented by the golang error, As well the agregation of an integer error code that simplify the error recognitions less the necessity of make reflections in the received object.

The first step to use this package is to get it through the command below, then you will be able to import it in your code.

go get github.com/finacore/commons-errors

To avoid the conflicts between packages during the imnport phase, is strongly recommended select other name for this package in the moment of import like shown below:

import (
	commonserrors "github.com/finacore/commons-errors"
)

Once imported you can use the package as you prefer. So read the function and methods documentation to know how to use this packagage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultError

type DefaultError struct {
	Message string `json:"message"`
	Code    int    `json:"-"`
}

DefaultError is a representaton of the most basic kind of error in this package. It's composed by a message description and an error code. By definition, the error code is not rendered when this structure is coverted to JSON.

func CreateDefaultError

func CreateDefaultError(message string) *DefaultError

CreateDefaultError returns a DefaultError object that contains the error message and the error code. By default the error code associated to this error is 500, but it's can be changed calling the method Status(int)

Usage:

err := commonserrors.CreateDefaultError("the error message goes here")

func MakeDefaultError

func MakeDefaultError(err error) *DefaultError

MakeDefaultError returns a DefaultError object that contains the error message and the error code. This method uses a preexistent error in order to capture their error message and calls the CreateDefaultError method.

As the CreateDefaultError method, this one also set the default status as 500, but it's can be changed calling the method Status(int)

Usage:

err := commonserrors.MakeDefaultError(previousError)

func (*DefaultError) Error

func (e *DefaultError) Error() string

Error method that returns a string literal containing the error message associated to the data structure.

Usage:

err := commonserrors.MakeDefaultError(previousError)
str := err.Error()

func (*DefaultError) Status

func (e *DefaultError) Status(status int) *DefaultError

Status method is responsable to overwrite the default error code that has set to the data structure and returns the own changed data structure.

Usage:

err := commonserrors.MakeDefaultError(previousError).Status(200)

type ValidationError

type ValidationError struct {
	Field string `json:"field"`
	DefaultError
}

ValidationError is a representaton of the error that coming from the validation library like go-playground/validator. It's composed by the DefaultError fields inheritance and the field name

func CreateValidationError

func CreateValidationError(field string, message string) *ValidationError

CreateValidationError returns a CreateValidationError object that contains the field name, the error message and the error code. By default the error code associated to this error is 400, but it's can be changed calling the method Status(int)

Usage:

err := commonserrors.CreateValidationError("name", "the name should not be empty")

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error method that returns a string literal containing the composition of the field name and the message separated by ': '

e.g. name: the name should not be empty

Usage:

err := commonserrors.CreateValidationError("name", "the name should not be empty")
str := err.Error()

func (*ValidationError) Status

func (e *ValidationError) Status(status int) *ValidationError

Status method is responsable to overwrite the default error code that has set to the data structure and returns the own changed data structure.

Usage:

err := commonserrors.CreateValidationError("name", "the name should not be empty").Status(200)

Jump to

Keyboard shortcuts

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