|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package azdext |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 11 | +) |
| 12 | + |
| 13 | +// ExtensionResponseError represents an HTTP response error returned from an extension over gRPC. |
| 14 | +// It mirrors azcore.ResponseError and preserves structured error information for telemetry purposes. |
| 15 | +type ExtensionResponseError struct { |
| 16 | + // Message is the human-readable error message |
| 17 | + Message string |
| 18 | + // Details contains additional error details |
| 19 | + Details string |
| 20 | + // ErrorCode is the error code from the service (e.g., "Conflict", "NotFound") |
| 21 | + ErrorCode string |
| 22 | + // StatusCode is the HTTP status code (e.g., 409, 404, 500) |
| 23 | + StatusCode int |
| 24 | + // ServiceName is the service name for telemetry (e.g., "ai.azure.com") |
| 25 | + ServiceName string |
| 26 | +} |
| 27 | + |
| 28 | +// Error implements the error interface |
| 29 | +func (e *ExtensionResponseError) Error() string { |
| 30 | + if e.Details != "" { |
| 31 | + return fmt.Sprintf("%s: %s", e.Message, e.Details) |
| 32 | + } |
| 33 | + return e.Message |
| 34 | +} |
| 35 | + |
| 36 | +// HasServiceInfo returns true if the error contains service information for telemetry |
| 37 | +func (e *ExtensionResponseError) HasServiceInfo() bool { |
| 38 | + return e.StatusCode > 0 && e.ServiceName != "" |
| 39 | +} |
| 40 | + |
| 41 | +// errorMessage defines the common interface for protobuf error messages |
| 42 | +// This allows us to write generic unwrap logic for any generated proto message |
| 43 | +type errorMessage interface { |
| 44 | + comparable |
| 45 | + GetMessage() string |
| 46 | + GetDetails() string |
| 47 | + GetErrorCode() string |
| 48 | + GetStatusCode() int32 |
| 49 | + GetServiceName() string |
| 50 | +} |
| 51 | + |
| 52 | +// errorInfo is a helper struct to hold extracted error information |
| 53 | +// before converting to a specific protobuf message type |
| 54 | +type errorInfo struct { |
| 55 | + message string |
| 56 | + details string |
| 57 | + errorCode string |
| 58 | + statusCode int32 |
| 59 | + service string |
| 60 | +} |
| 61 | + |
| 62 | +// captureErrorInfo extracts structured error information from a Go error. |
| 63 | +// It handles nil errors, ExtensionResponseError, and azcore.ResponseError. |
| 64 | +func captureErrorInfo(err error) errorInfo { |
| 65 | + if err == nil { |
| 66 | + return errorInfo{} |
| 67 | + } |
| 68 | + |
| 69 | + // Default to the error string |
| 70 | + info := errorInfo{message: err.Error()} |
| 71 | + |
| 72 | + // If it's already an ExtensionResponseError, preserve all fields including Details |
| 73 | + var extErr *ExtensionResponseError |
| 74 | + if errors.As(err, &extErr) { |
| 75 | + info.message = extErr.Message |
| 76 | + info.details = extErr.Details |
| 77 | + info.errorCode = extErr.ErrorCode |
| 78 | + info.statusCode = int32(extErr.StatusCode) |
| 79 | + info.service = extErr.ServiceName |
| 80 | + return info |
| 81 | + } |
| 82 | + |
| 83 | + // Try to extract structured error information from Azure SDK errors |
| 84 | + var respErr *azcore.ResponseError |
| 85 | + if errors.As(err, &respErr) { |
| 86 | + info.errorCode = respErr.ErrorCode |
| 87 | + info.statusCode = int32(respErr.StatusCode) |
| 88 | + if respErr.RawResponse != nil && respErr.RawResponse.Request != nil { |
| 89 | + info.service = respErr.RawResponse.Request.Host |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return info |
| 94 | +} |
| 95 | + |
| 96 | +// WrapErrorForServiceTarget wraps a Go error into a ServiceTargetErrorMessage for transmission over gRPC. |
| 97 | +func WrapErrorForServiceTarget(err error) *ServiceTargetErrorMessage { |
| 98 | + info := captureErrorInfo(err) |
| 99 | + if info.message == "" { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + return &ServiceTargetErrorMessage{ |
| 104 | + Message: info.message, |
| 105 | + Details: info.details, |
| 106 | + ErrorCode: info.errorCode, |
| 107 | + StatusCode: info.statusCode, |
| 108 | + ServiceName: info.service, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// WrapErrorForFrameworkService wraps a Go error into a FrameworkServiceErrorMessage for transmission over gRPC. |
| 113 | +func WrapErrorForFrameworkService(err error) *FrameworkServiceErrorMessage { |
| 114 | + info := captureErrorInfo(err) |
| 115 | + if info.message == "" { |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + return &FrameworkServiceErrorMessage{ |
| 120 | + Message: info.message, |
| 121 | + Details: info.details, |
| 122 | + ErrorCode: info.errorCode, |
| 123 | + StatusCode: info.statusCode, |
| 124 | + ServiceName: info.service, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// unwrapError is a generic helper to convert protobuf error messages back to Go errors |
| 129 | +func unwrapError[T errorMessage](msg T) error { |
| 130 | + var zero T |
| 131 | + if msg == zero || msg.GetMessage() == "" { |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + return &ExtensionResponseError{ |
| 136 | + Message: msg.GetMessage(), |
| 137 | + Details: msg.GetDetails(), |
| 138 | + ErrorCode: msg.GetErrorCode(), |
| 139 | + StatusCode: int(msg.GetStatusCode()), |
| 140 | + ServiceName: msg.GetServiceName(), |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// UnwrapErrorFromServiceTarget converts a ServiceTargetErrorMessage back to a Go error. |
| 145 | +func UnwrapErrorFromServiceTarget(msg *ServiceTargetErrorMessage) error { |
| 146 | + return unwrapError(msg) |
| 147 | +} |
| 148 | + |
| 149 | +// UnwrapErrorFromFrameworkService converts a FrameworkServiceErrorMessage back to a Go error. |
| 150 | +func UnwrapErrorFromFrameworkService(msg *FrameworkServiceErrorMessage) error { |
| 151 | + return unwrapError(msg) |
| 152 | +} |
0 commit comments