|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/google/go-github/v72/github" |
| 8 | + "github.com/mark3labs/mcp-go/mcp" |
| 9 | +) |
| 10 | + |
| 11 | +type GitHubAPIError struct { |
| 12 | + Message string `json:"message"` |
| 13 | + Response *github.Response `json:"-"` |
| 14 | + Err error `json:"-"` |
| 15 | +} |
| 16 | + |
| 17 | +// NewGitHubAPIError creates a new GitHubAPIError with the provided message, response, and error. |
| 18 | +func newGitHubAPIError(message string, resp *github.Response, err error) *GitHubAPIError { |
| 19 | + return &GitHubAPIError{ |
| 20 | + Message: message, |
| 21 | + Response: resp, |
| 22 | + Err: err, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (e *GitHubAPIError) Error() string { |
| 27 | + return fmt.Errorf("%s: %w", e.Message, e.Err).Error() |
| 28 | +} |
| 29 | + |
| 30 | +type GitHubGraphQLError struct { |
| 31 | + Message string `json:"message"` |
| 32 | + Err error `json:"-"` |
| 33 | +} |
| 34 | + |
| 35 | +func newGitHubGraphQLError(message string, err error) *GitHubGraphQLError { |
| 36 | + return &GitHubGraphQLError{ |
| 37 | + Message: message, |
| 38 | + Err: err, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (e *GitHubGraphQLError) Error() string { |
| 43 | + return fmt.Errorf("%s: %w", e.Message, e.Err).Error() |
| 44 | +} |
| 45 | + |
| 46 | +type GitHubErrorKey struct{} |
| 47 | +type GitHubCtxErrors struct { |
| 48 | + api []*GitHubAPIError |
| 49 | + graphQL []*GitHubGraphQLError |
| 50 | +} |
| 51 | + |
| 52 | +// ContextWithGitHubErrors updates or creates a context with a pointer to GitHub error information (to be used by middleware). |
| 53 | +func ContextWithGitHubErrors(ctx context.Context) context.Context { |
| 54 | + if ctx == nil { |
| 55 | + ctx = context.Background() |
| 56 | + } |
| 57 | + if val, ok := ctx.Value(GitHubErrorKey{}).(*GitHubCtxErrors); ok { |
| 58 | + // If the context already has GitHubCtxErrors, we just empty the slices to start fresh |
| 59 | + val.api = []*GitHubAPIError{} |
| 60 | + val.graphQL = []*GitHubGraphQLError{} |
| 61 | + } else { |
| 62 | + // If not, we create a new GitHubCtxErrors and set it in the context |
| 63 | + ctx = context.WithValue(ctx, GitHubErrorKey{}, &GitHubCtxErrors{}) |
| 64 | + } |
| 65 | + |
| 66 | + return ctx |
| 67 | +} |
| 68 | + |
| 69 | +// GetGitHubAPIErrors retrieves the slice of GitHubAPIErrors from the context. |
| 70 | +func GetGitHubAPIErrors(ctx context.Context) ([]*GitHubAPIError, error) { |
| 71 | + if val, ok := ctx.Value(GitHubErrorKey{}).(*GitHubCtxErrors); ok { |
| 72 | + return val.api, nil // return the slice of API errors from the context |
| 73 | + } |
| 74 | + return nil, fmt.Errorf("context does not contain GitHubCtxErrors") |
| 75 | +} |
| 76 | + |
| 77 | +// GetGitHubGraphQLErrors retrieves the slice of GitHubGraphQLErrors from the context. |
| 78 | +func GetGitHubGraphQLErrors(ctx context.Context) ([]*GitHubGraphQLError, error) { |
| 79 | + if val, ok := ctx.Value(GitHubErrorKey{}).(*GitHubCtxErrors); ok { |
| 80 | + return val.graphQL, nil // return the slice of GraphQL errors from the context |
| 81 | + } |
| 82 | + return nil, fmt.Errorf("context does not contain GitHubCtxErrors") |
| 83 | +} |
| 84 | + |
| 85 | +func NewGitHubAPIErrorToCtx(ctx context.Context, message string, resp *github.Response, err error) (context.Context, error) { |
| 86 | + apiErr := newGitHubAPIError(message, resp, err) |
| 87 | + if ctx != nil { |
| 88 | + _, _ = addGitHubAPIErrorToContext(ctx, apiErr) // Explicitly ignore error for graceful handling |
| 89 | + } |
| 90 | + return ctx, nil |
| 91 | +} |
| 92 | + |
| 93 | +func addGitHubAPIErrorToContext(ctx context.Context, err *GitHubAPIError) (context.Context, error) { |
| 94 | + if val, ok := ctx.Value(GitHubErrorKey{}).(*GitHubCtxErrors); ok { |
| 95 | + val.api = append(val.api, err) // append the error to the existing slice in the context |
| 96 | + return ctx, nil |
| 97 | + } |
| 98 | + return nil, fmt.Errorf("context does not contain GitHubCtxErrors") |
| 99 | +} |
| 100 | + |
| 101 | +func addGitHubGraphQLErrorToContext(ctx context.Context, err *GitHubGraphQLError) (context.Context, error) { |
| 102 | + if val, ok := ctx.Value(GitHubErrorKey{}).(*GitHubCtxErrors); ok { |
| 103 | + val.graphQL = append(val.graphQL, err) // append the error to the existing slice in the context |
| 104 | + return ctx, nil |
| 105 | + } |
| 106 | + return nil, fmt.Errorf("context does not contain GitHubCtxErrors") |
| 107 | +} |
| 108 | + |
| 109 | +// NewGitHubAPIErrorResponse returns an mcp.NewToolResultError and retains the error in the context for access via middleware |
| 110 | +func NewGitHubAPIErrorResponse(ctx context.Context, message string, resp *github.Response, err error) *mcp.CallToolResult { |
| 111 | + apiErr := newGitHubAPIError(message, resp, err) |
| 112 | + if ctx != nil { |
| 113 | + _, _ = addGitHubAPIErrorToContext(ctx, apiErr) // Explicitly ignore error for graceful handling |
| 114 | + } |
| 115 | + return mcp.NewToolResultErrorFromErr(message, err) |
| 116 | +} |
| 117 | + |
| 118 | +// NewGitHubGraphQLErrorResponse returns an mcp.NewToolResultError and retains the error in the context for access via middleware |
| 119 | +func NewGitHubGraphQLErrorResponse(ctx context.Context, message string, err error) *mcp.CallToolResult { |
| 120 | + graphQLErr := newGitHubGraphQLError(message, err) |
| 121 | + if ctx != nil { |
| 122 | + _, _ = addGitHubGraphQLErrorToContext(ctx, graphQLErr) // Explicitly ignore error for graceful handling |
| 123 | + } |
| 124 | + return mcp.NewToolResultErrorFromErr(message, err) |
| 125 | +} |
0 commit comments