micro-editor/micro · error
Invalid clipboard method
Error message
Invalid clipboard method
What it means
Returned by clipboard.read(r, m) when the Method value passed to the switch is none of External, Internal, or Terminal. In practice CurrentMethod is only ever assigned those three constants, so this is a programming-error sentinel for callers who construct a Method directly (e.g. clipboard.Method(9)) or who add a new Method constant without extending read/write switches in internal/clipboard/clipboard.go.
Source
Thrown at internal/clipboard/clipboard.go:136
return string(b), e
default:
return internal.read(r), nil
}
case Internal:
return internal.read(r), nil
case Terminal:
switch r {
case ClipboardReg:
// terminal paste works by sending an esc sequence to the
// terminal to trigger a paste event
return terminal.read("clipboard")
case PrimaryReg:
return terminal.read("primary")
default:
return internal.read(r), nil
}
}
return "", errors.New("Invalid clipboard method")
}
func write(text string, r Register, m Method) error {
switch m {
case External:
switch r {
case ClipboardReg:
return clipboard.WriteAll(clipper.RegClipboard, []byte(text))
case PrimaryReg:
return clipboard.WriteAll(clipper.RegPrimary, []byte(text))
default:
internal.write(text, r)
}
case Internal:
internal.write(text, r)
case Terminal:
switch r {
case ClipboardReg:View on GitHub (pinned to 1c8b82b32e)
Solutions
- Pass only constants: clipboard.External, clipboard.Internal, or clipboard.Terminal.
- If you added a new Method constant, add its case to both read() and write() switches in internal/clipboard/clipboard.go.
- Set the method by name via clipboard.SetMethod("internal"|"external"|"terminal") which can never produce an invalid value.
- Guard with a range check (see validation) before calling Read/Write in wrapper code.
Example fix
// before:
m := clipboard.Method(cfgInt) // cfgInt == 7 -> falls through
_, err := clipboard.Read(clipboard.ClipboardReg) // err: Invalid clipboard method
// after:
switch cfgStr {
case "external", "terminal", "internal":
clipboard.SetMethod(cfgStr)
default:
clipboard.SetMethod("internal")
}
_, err := clipboard.Read(clipboard.ClipboardReg) Defensive patterns
Strategy: validation
Validate before calling
func validMethod(m clipboard.Method) bool {
return m == clipboard.External || m == clipboard.Internal || m == clipboard.Terminal
}
if !validMethod(m) { m = clipboard.Internal } Type guard
func isValidMethod(m clipboard.Method) bool {
switch m {
case clipboard.External, clipboard.Internal, clipboard.Terminal:
return true
}
return false
} Try / catch
text, err := clipboard.Read(clipboard.ClipboardReg)
if err != nil && err.Error() == "Invalid clipboard method" {
text, err = clipboard.Read /* after clipboard.SetMethod("internal") */
} Prevention
- Always obtain a Method via clipboard.SetMethod(name) with a whitelisted string, never by casting ints.
- If you add a Method constant, grep the package switches in read()/write() and extend both.
- Default unknown config values to "internal" before calling SetMethod.
When it happens
Trigger: Calling the unexported read via a package refactor with an out-of-range Method, or clipboard.Initialize/SetMethod flow bypassed so CurrentMethod holds a zero-value-altered integer. Note SetMethod silently ignores unknown strings (keeps previous value), so a bad "clipboard" setting does NOT produce this error — only an invalid Method integer does.
Common situations: Library consumers embedding micro's clipboard package and passing a Method read from config as a raw int; developers adding a fourth clipboard backend (e.g. Wayland-native) and forgetting one switch statement.
Related errors
AI-assisted analysis of micro-editor/micro@1c8b82b32e (2026-08-15).
Data as JSON: /api/errors/ffefb7f3dff30af5.
Report an issue: GitHub.