package i18n import ( "errors" "regexp" "strings" "sync" "unicode/utf8" ) const maxRawDetailBytes = 8192 var ( reErrMac = regexp.MustCompile(`(?i)\b(mac(?:_?address)?[\s:=]+)["']?[0-9a-f]{2}(?:[:-][0-9a-f]{2}){5}["']?`) reErrSerialKV = regexp.MustCompile(`(?i)\b((?:system)?serial(?:_?number)?\s*[:=]\s*|mlb\s*[:=]\s*|smuuid\s*[:=]\s*)["']?[A-Za-z0-9-]{8,}["']?`) reErrSerialSp = regexp.MustCompile(`(?i)\b(serial\s+)["']?(?-i:[A-Z0-9]{8,})["']?`) reErrOSK = regexp.MustCompile(`(?i)\b(osk\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,"']+)`) reErrBearer = regexp.MustCompile(`(?i)((?:Bearer|Basic|Authorization:\s*token)\s+)[A-Za-z0-9._~+/=-]{16,}`) reErrTokenKV = regexp.MustCompile(`(?i)((?:REIMS_TOKEN|GITHUB_TOKEN|access_token|auth_token|token|"token")\s*[:=]\s*["']?)[A-Za-z0-9._~+/-]{16,}(["']?)`) detailRedactMu sync.RWMutex detailRedactFn func(string) string ) // SetDetailRedactor registers an optional additional redactor function applied // to CodedError.RawDetail before diagnostic text crosses Wails IPC or CLI stderr. func SetDetailRedactor(fn func(string) string) { detailRedactMu.Lock() detailRedactFn = fn detailRedactMu.Unlock() } func sanitizeRawDetail(s string) string { s = strings.TrimSpace(s) if s == "" { return "" } lines := strings.Split(s, "\n") for i, l := range lines { l = reErrMac.ReplaceAllString(l, "${1}[redacted]") l = reErrSerialKV.ReplaceAllString(l, "${1}[redacted]") l = reErrSerialSp.ReplaceAllString(l, "${1}[redacted]") l = reErrOSK.ReplaceAllString(l, "${1}[redacted]") l = reErrBearer.ReplaceAllString(l, "${1}[redacted]") l = reErrTokenKV.ReplaceAllString(l, "${1}[redacted]${2}") lines[i] = l } s = strings.Join(lines, "\n") detailRedactMu.RLock() fn := detailRedactFn detailRedactMu.RUnlock() if fn != nil { s = fn(s) } if len(s) > maxRawDetailBytes { cut := maxRawDetailBytes for cut > 0 && !utf8.RuneStart(s[cut]) { cut-- } s = s[:cut] + "…" } return s } // CodedError represents a user-facing error with a stable supportable error code // (e.g. "E_VM_CLONE_RUNNING"), a localized message key + named params, and an // optional redacted raw technical detail string for collapsible display and GitHub // Issue diagnostics (see docs/design-guidelines.md and AGENTS.md). type CodedError struct { Locale Locale Code string Key string Params Vars RawDetail string Cause error } // NewCodedError constructs a localized, supportable, secret-redacted CodedError. func NewCodedError(loc Locale, code, key string, params Vars, cause error) *CodedError { raw := "" if cause != nil { raw = sanitizeRawDetail(cause.Error()) } return &CodedError{ Locale: loc, Code: code, Key: key, Params: params, RawDetail: raw, Cause: cause, } } // WithDetail attaches explicit raw diagnostic text (e.g. stderr from qemu-img or reims-ctl), // automatically redacting sensitive tokens/OSK/SMBIOS/MAC values and capping length. func (e *CodedError) WithDetail(detail string) *CodedError { if e != nil { e.RawDetail = sanitizeRawDetail(detail) } return e } // Summary returns the localized human-readable summary in loc (or e.Locale if loc is empty). func (e *CodedError) Summary(loc Locale) string { if e == nil { return "" } target := loc if target == "" { target = e.Locale } return M(target, e.Key, e.Params) } // Error formats the error cleanly for both Wails Promise rejection decoding // (" [E_CODE]\n[detail] ") and plain terminal/log readers. func (e *CodedError) Error() string { if e == nil { return "" } msg := e.Summary(e.Locale) if e.Code != "" { msg += " [" + e.Code + "]" } if e.RawDetail != "" && !strings.Contains(msg, e.RawDetail) { msg += "\n[detail] " + e.RawDetail } return msg } func (e *CodedError) Unwrap() error { if e == nil { return nil } return e.Cause } // FormatCLIError formats any error cleanly for CLI stderr output, translating // CodedError into the requested CLI locale and indenting technical details. func FormatCLIError(loc Locale, err error) string { if err == nil { return "" } var ce *CodedError if errors.As(err, &ce) { line := ce.Summary(loc) if ce.Code != "" { line = "[" + ce.Code + "] " + line } if ce.RawDetail != "" { line += "\n detail: " + strings.ReplaceAll(ce.RawDetail, "\n", "\n ") } return line } return sanitizeRawDetail(err.Error()) }