common

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: Apache-2.0 Imports: 13 Imported by: 68

Documentation

Index

Constants

View Source
const (
	UnAuthenticated     = "E100"
	UnAuthorized        = "E101"
	BadRequest          = "E102"
	InternalServerError = "E103"
	ResourceNotFound    = "E104"
	UnknownError        = "E105"
	CONTENT_DISPOSITION = "Content-Disposition"
	CONTENT_TYPE        = "Content-Type"
	CONTENT_LENGTH      = "Content-Length"
	APPLICATION_JSON    = "application/json"
)
View Source
const TokenHeaderKey = "token"
View Source
const UnAuthorisedUser = "Unauthorized User"

Variables

View Source
var ErrUnAuthorized = errors.New("unauthorized user")

ErrUnAuthorized is the error object for unauthorized user. Use this error object when user is not authorized to perform an action.

Functions

func ErrorMessage added in v0.4.24

func ErrorMessage(code string) string

func ExtractBoolQueryParam added in v0.7.2

func ExtractBoolQueryParam(r *http.Request, paramName string) (bool, error)

func ExtractIntArrayFromQueryParam added in v0.7.2

func ExtractIntArrayFromQueryParam(r *http.Request, paramName string) ([]int, error)

ExtractIntArrayFromQueryParam returns list of all ids in []int extracted from query param use this method over ExtractIntArrayQueryParam if there is list of query params

func ExtractIntArrayQueryParam added in v0.7.0

func ExtractIntArrayQueryParam(w http.ResponseWriter, r *http.Request, paramName string) ([]int, error)

ExtractIntArrayQueryParam don't use this func, doesn't handle all cases to capture query params use ExtractIntArrayFromQueryParam over this func to capture int array from query param.

func ExtractIntPathParam added in v0.7.0

func ExtractIntPathParam(w http.ResponseWriter, r *http.Request, paramName string) (int, error)

func ExtractIntPathParamWithContext added in v1.8.0

func ExtractIntPathParamWithContext(w http.ResponseWriter, r *http.Request, paramName string, resourceType string) (int, error)

ExtractIntPathParamWithContext provides enhanced error messages with resource context

func ExtractIntQueryParam added in v0.7.0

func ExtractIntQueryParam(w http.ResponseWriter, r *http.Request, paramName string, defaultValue int) (int, error)

func HandleForbidden added in v1.8.0

func HandleForbidden(w http.ResponseWriter, r *http.Request, resource string)

HandleForbidden handles forbidden access errors

func HandleParameterError added in v1.8.0

func HandleParameterError(w http.ResponseWriter, r *http.Request, paramName, paramValue string)

HandleParameterError handles path parameter validation errors

func HandleResourceNotFound added in v1.8.0

func HandleResourceNotFound(w http.ResponseWriter, r *http.Request, resourceType, resourceID string)

HandleResourceNotFound handles resource not found errors

func HandleUnauthorized added in v1.8.0

func HandleUnauthorized(w http.ResponseWriter, r *http.Request)

HandleUnauthorized handles unauthorized access errors

func HandleValidationError added in v1.8.0

func HandleValidationError(w http.ResponseWriter, r *http.Request, fieldName, message string)

HandleValidationError handles validation errors

func HandleValidationErrors added in v1.8.0

func HandleValidationErrors(w http.ResponseWriter, r *http.Request, err error)

HandleValidationErrors handles multiple validation errors

func WriteApiJsonResponse added in v0.4.24

func WriteApiJsonResponse(w http.ResponseWriter, result interface{}, statusCode int, errCode string, errorMessage string)

func WriteApiJsonResponseStructured added in v0.4.24

func WriteApiJsonResponseStructured(w http.ResponseWriter, apiResponse *ApiResponse, statusCode int)

func WriteDatabaseError added in v1.8.0

func WriteDatabaseError(w http.ResponseWriter, operation string, err error)

func WriteForbiddenError added in v1.8.0

func WriteForbiddenError(w http.ResponseWriter, operation string, resource string)

func WriteInvalidAppIdError added in v1.8.0

func WriteInvalidAppIdError(w http.ResponseWriter, appId string)

func WriteJsonResp

func WriteJsonResp(w http.ResponseWriter, err error, respBody interface{}, status int)

use of writeJsonRespStructured is preferable. it api exists due to historical reason err.message is used as internal message for ApiError object in resp

func WriteJsonRespWithResourceContext added in v1.8.0

func WriteJsonRespWithResourceContext(w http.ResponseWriter, err error, respBody interface{},
	status int, resourceType, resourceId string)

WriteJsonRespWithResourceContext enhances error response with resource context This function provides better error messages for database errors by including resource context

func WriteJsonRespWithResourceContextFromId added in v1.8.0

func WriteJsonRespWithResourceContextFromId(w http.ResponseWriter, err error, respBody interface{},
	status int, resourceType string, resourceId int)

WriteJsonRespWithResourceContextFromId is a convenience function for integer IDs

func WriteMissingRequiredFieldError added in v1.8.0

func WriteMissingRequiredFieldError(w http.ResponseWriter, fieldName string)

Convenience functions for common error scenarios (to fix build errors)

func WriteOctetStreamResp added in v0.6.23

func WriteOctetStreamResp(w http.ResponseWriter, r *http.Request, byteArr []byte, defaultFilename string)

func WritePipelineNotFoundError added in v1.8.0

func WritePipelineNotFoundError(w http.ResponseWriter, pipelineId int)

func WriteSpecificErrorResponse added in v1.8.0

func WriteSpecificErrorResponse(w http.ResponseWriter, errorCode string, message string, details []string, statusCode int)

func WriteUnauthorizedError added in v1.8.0

func WriteUnauthorizedError(w http.ResponseWriter)

func WriteValidationError added in v1.8.0

func WriteValidationError(w http.ResponseWriter, fieldName string, message string)

Types

type ApiResponse added in v0.4.24

type ApiResponse struct {
	Success bool           `json:"success,notnull" validate:"required"`
	Error   *ErrorResponse `json:"error,omitempty"`
	Result  interface{}    `json:"result,omitempty"`
}

type ErrorResponse added in v0.4.24

type ErrorResponse struct {
	Code    string `json:"code,notnull" validate:"required"`
	Message string `json:"message,notnull" validate:"required"`
}

type ErrorResponseBuilder added in v1.8.0

type ErrorResponseBuilder struct {
	// contains filtered or unexported fields
}

ErrorResponseBuilder provides a fluent interface for building error responses

func NewErrorResponseBuilder added in v1.8.0

func NewErrorResponseBuilder(w http.ResponseWriter, r *http.Request) *ErrorResponseBuilder

NewErrorResponseBuilder creates a new error response builder

func (*ErrorResponseBuilder) HandleError added in v1.8.0

func (erb *ErrorResponseBuilder) HandleError(err error)

HandleError processes an error and writes an appropriate response

func (*ErrorResponseBuilder) HandleSuccess added in v1.8.0

func (erb *ErrorResponseBuilder) HandleSuccess(data interface{})

HandleSuccess writes a successful response

func (*ErrorResponseBuilder) WithOperation added in v1.8.0

func (erb *ErrorResponseBuilder) WithOperation(operation string) *ErrorResponseBuilder

WithOperation sets the operation context for better error messages

func (*ErrorResponseBuilder) WithResource added in v1.8.0

func (erb *ErrorResponseBuilder) WithResource(resourceType, resourceID string) *ErrorResponseBuilder

WithResource sets the resource context for better error messages

func (*ErrorResponseBuilder) WithResourceFromId added in v1.8.0

func (erb *ErrorResponseBuilder) WithResourceFromId(resourceType string, resourceID int) *ErrorResponseBuilder

WithResourceFromId sets the resource context with an integer ID

type Response

type Response struct {
	Code   int              `json:"code,omitempty"`
	Status string           `json:"status,omitempty"`
	Result interface{}      `json:"result,omitempty"`
	Errors []*util.ApiError `json:"errors,omitempty"`
}

global response body used across api

Jump to

Keyboard shortcuts

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