MCPcopy Create free account
hub / github.com/Permify/permify / HandleError

Function HandleError

internal/storage/postgres/utils/common.go:121–144  ·  view source on GitHub ↗

HandleError records an error in the given span, logs the error, and returns a standardized error. This function is used for consistent error handling across different parts of the application.

(ctx context.Context, span trace.Span, err error, errorCode base.ErrorCode)

Source from the content-addressed store, hash-verified

119// HandleError records an error in the given span, logs the error, and returns a standardized error.
120// This function is used for consistent error handling across different parts of the application.
121func HandleError(ctx context.Context, span trace.Span, err error, errorCode base.ErrorCode) error {
122 // Check if the error is context-related
123 if IsContextRelatedError(ctx, err) {
124 slog.DebugContext(ctx, "A context-related error occurred",
125 slog.String("error", err.Error()))
126 return errors.New(base.ErrorCode_ERROR_CODE_CANCELLED.String())
127 }
128
129 // Check if the error is serialization-related
130 if IsSerializationRelatedError(err) {
131 slog.DebugContext(ctx, "A serialization-related error occurred",
132 slog.String("error", err.Error()))
133 return errors.New(base.ErrorCode_ERROR_CODE_SERIALIZATION.String())
134 }
135
136 // For all other types of errors, log them at the error level and record them in the span
137 slog.ErrorContext(ctx, "An operational error occurred",
138 slog.Any("error", err))
139 span.RecordError(err)
140 span.SetStatus(codes.Error, err.Error())
141
142 // Return a new error with the standard error code provided
143 return errors.New(errorCode.String())
144}
145
146// IsContextRelatedError checks if the error is due to context cancellation, deadline exceedance, or closed connection
147func IsContextRelatedError(ctx context.Context, err error) bool {

Callers 15

CreateTenantMethod · 0.92
DeleteTenantMethod · 0.92
QueryRelationshipsMethod · 0.92
ReadRelationshipsMethod · 0.92
QuerySingleAttributeMethod · 0.92
QueryAttributesMethod · 0.92
ReadAttributesMethod · 0.92
HeadSnapshotMethod · 0.92
WriteMethod · 0.92
DeleteMethod · 0.92
RunBundleMethod · 0.92

Calls 4

IsContextRelatedErrorFunction · 0.70
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected