Documentation
¶
Index ¶
- Constants
- Variables
- func ErrorMessage(code string) string
- func ExtractBoolQueryParam(r *http.Request, paramName string) (bool, error)
- func ExtractIntArrayFromQueryParam(r *http.Request, paramName string) ([]int, error)
- func ExtractIntArrayQueryParam(w http.ResponseWriter, r *http.Request, paramName string) ([]int, error)
- func ExtractIntPathParam(w http.ResponseWriter, r *http.Request, paramName string) (int, error)
- func ExtractIntPathParamWithContext(w http.ResponseWriter, r *http.Request, paramName string, resourceType string) (int, error)
- func ExtractIntQueryParam(w http.ResponseWriter, r *http.Request, paramName string, defaultValue int) (int, error)
- func HandleForbidden(w http.ResponseWriter, r *http.Request, resource string)
- func HandleParameterError(w http.ResponseWriter, r *http.Request, paramName, paramValue string)
- func HandleResourceNotFound(w http.ResponseWriter, r *http.Request, resourceType, resourceID string)
- func HandleUnauthorized(w http.ResponseWriter, r *http.Request)
- func HandleValidationError(w http.ResponseWriter, r *http.Request, fieldName, message string)
- func HandleValidationErrors(w http.ResponseWriter, r *http.Request, err error)
- func WriteApiJsonResponse(w http.ResponseWriter, result interface{}, statusCode int, errCode string, ...)
- func WriteApiJsonResponseStructured(w http.ResponseWriter, apiResponse *ApiResponse, statusCode int)
- func WriteDatabaseError(w http.ResponseWriter, operation string, err error)
- func WriteForbiddenError(w http.ResponseWriter, operation string, resource string)
- func WriteInvalidAppIdError(w http.ResponseWriter, appId string)
- func WriteJsonResp(w http.ResponseWriter, err error, respBody interface{}, status int)
- func WriteJsonRespWithResourceContext(w http.ResponseWriter, err error, respBody interface{}, status int, ...)
- func WriteJsonRespWithResourceContextFromId(w http.ResponseWriter, err error, respBody interface{}, status int, ...)
- func WriteMissingRequiredFieldError(w http.ResponseWriter, fieldName string)
- func WriteOctetStreamResp(w http.ResponseWriter, r *http.Request, byteArr []byte, defaultFilename string)
- func WritePipelineNotFoundError(w http.ResponseWriter, pipelineId int)
- func WriteSpecificErrorResponse(w http.ResponseWriter, errorCode string, message string, details []string, ...)
- func WriteUnauthorizedError(w http.ResponseWriter)
- func WriteValidationError(w http.ResponseWriter, fieldName string, message string)
- type ApiResponse
- type ErrorResponse
- type ErrorResponseBuilder
- func (erb *ErrorResponseBuilder) HandleError(err error)
- func (erb *ErrorResponseBuilder) HandleSuccess(data interface{})
- func (erb *ErrorResponseBuilder) WithOperation(operation string) *ErrorResponseBuilder
- func (erb *ErrorResponseBuilder) WithResource(resourceType, resourceID string) *ErrorResponseBuilder
- func (erb *ErrorResponseBuilder) WithResourceFromId(resourceType string, resourceID int) *ErrorResponseBuilder
- type Response
Constants ¶
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" )
const TokenHeaderKey = "token"
const UnAuthorisedUser = "Unauthorized User"
Variables ¶
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 ExtractBoolQueryParam ¶ added in v0.7.2
func ExtractIntArrayFromQueryParam ¶ added in v0.7.2
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 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 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 WritePipelineNotFoundError ¶ added in v1.8.0
func WritePipelineNotFoundError(w http.ResponseWriter, pipelineId int)
func WriteSpecificErrorResponse ¶ added in v1.8.0
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 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