Files
larksuite-cli/shortcuts/markdown/markdown_errors.go
evandance e751a53f76 feat(markdown): emit typed error envelopes across the markdown domain (#1347)
Emit structured validation, API, network, file, and internal error envelopes for Markdown shortcuts so users and agents can recover from failed markdown workflows using stable type, subtype, param, and code fields.

Add Markdown domain errscontract and golangci guards to prevent legacy envelope and common helper regressions.
2026-06-10 17:42:18 +08:00

54 lines
1.8 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package markdown
import (
"errors"
"github.com/larksuite/cli/errs"
)
func markdownValidationError(format string, args ...any) *errs.ValidationError {
return errs.NewValidationError(errs.SubtypeInvalidArgument, format, args...)
}
func markdownValidationParamError(param, format string, args ...any) *errs.ValidationError {
return markdownValidationError(format, args...).WithParam(param)
}
func markdownInvalidParam(name, reason string) errs.InvalidParam {
return errs.InvalidParam{Name: name, Reason: reason}
}
// withMarkdownFileParam tags a validation failure with the originating flag
// when it does not already name one. Shared input-file helpers such as
// common.WrapInputStatErrorTyped cannot know which flag supplied the path, so
// the caller attaches it here to keep the recoverable param on the wire.
func withMarkdownFileParam(err error, param string) error {
if err == nil || param == "" {
return err
}
var ve *errs.ValidationError
if errors.As(err, &ve) && ve.Param == "" {
ve.WithParam(param)
}
return err
}
// wrapMarkdownDownloadError classifies a download failure. An already-typed
// error keeps its carrier — type, subtype, code and extensions — so callers see
// the upstream classification: a validation problem passes through verbatim,
// any other problem gains a "download failed" prefix for operation context.
// An untyped error becomes a network transport error carrying the original as
// its cause.
func wrapMarkdownDownloadError(err error) error {
if p, ok := errs.ProblemOf(err); ok {
if p.Category != errs.CategoryValidation {
p.Message = "download failed: " + p.Message
}
return err
}
return errs.NewNetworkError(errs.SubtypeNetworkTransport, "download failed: %s", err).WithCause(err)
}