{"id":"40c6e505bf3e959a","repo":"stretchr/testify","slug":"assert-arguments-error-d-failed-because-object","errorCode":null,"errorMessage":"assert: arguments: Error(%d) failed because object wasn't correct type: %v","messagePattern":"assert: arguments: Error\\((.+?)\\) failed because object wasn't correct type: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":1132,"sourceCode":"\tvar s int\n\tvar ok bool\n\tif s, ok = args.Get(index).(int); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Int(%d) failed because object wasn't correct type: %v\", index, args.Get(index)))\n\t}\n\treturn s\n}\n\n// Error gets the argument at the specified index. Panics if there is no argument, or\n// if the argument is of the wrong type.\nfunc (args Arguments) Error(index int) error {\n\tobj := args.Get(index)\n\tvar s error\n\tvar ok bool\n\tif obj == nil {\n\t\treturn nil\n\t}\n\tif s, ok = obj.(error); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Error(%d) failed because object wasn't correct type: %v\", index, obj))\n\t}\n\treturn s\n}\n\n// Bool gets the argument at the specified index. Panics if there is no argument, or\n// if the argument is of the wrong type.\nfunc (args Arguments) Bool(index int) bool {\n\tvar s bool\n\tvar ok bool\n\tif s, ok = args.Get(index).(bool); !ok {\n\t\tpanic(fmt.Sprintf(\"assert: arguments: Bool(%d) failed because object wasn't correct type: %v\", index, args.Get(index)))\n\t}\n\treturn s\n}\n\n// safeTypeName returns the reflect.Type's name without causing a panic.\n// If the provided reflect.Type is nil, it returns the placeholder string \"<nil>\"\nfunc safeTypeName(t reflect.Type) string {","sourceCodeStart":1114,"sourceCodeEnd":1150,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L1114-L1150","documentation":"Panicked by Arguments.Error(index) (mock/mock.go:1124) when args.Get(index) is non-nil and does not type-assert to the builtin error interface. Note: nil is handled (returns nil error at mock/mock.go:1128-1130); the panic only fires for present-but-wrong types. Message prints the offending value.","triggerScenarios":"Calling args.Error(0) when the argument is a non-error value (string, struct, *http.Request) or a custom error type that doesn't satisfy the error interface. Also when the method returns/accepts a wrapped error type that was passed as a concrete struct.","commonSituations":"Method signature changed so a positional argument is no longer an error; using a custom result type that embeds but isn't exposed as error; asserting on the wrong index.","solutions":["Verify the argument actually implements error before calling args.Error, using a type switch.","Fix the index so it points at the real error-typed parameter.","If the value is a wrapper, expose it via an Err() method or assert on the concrete type then extract."],"exampleFix":"// before\nerr := args.Error(0) // arg is a custom Result\n// after\nswitch v := args.Get(0).(type) {\ncase error: err = v\ncase Result: err = v.Err()\n}","handlingStrategy":"type-guard","validationCode":"func argIsError(args mock.Arguments, i int) bool {\n    if i >= len(args) { return true } // nil is allowed\n    if v := args.Get(i); v != nil {\n        _, ok := v.(error); return ok\n    }\n    return true\n}\n// before args.Error(i): if !argIsError(args, i) { t.Fatal(\"arg not error\") }","typeGuard":"func argIsError(args mock.Arguments, i int) bool {\n    if i >= len(args) { return true }\n    if v := args.Get(i); v != nil {\n        _, ok := v.(error); return ok\n    }\n    return true\n}","tryCatchPattern":"// Use a type switch to safely extract:\nswitch v := args.Get(i).(type) {\ncase nil: err = nil\ncase error: err = v\ndefault: t.Fatalf(\"unexpected type %T\", v)\n}","preventionTips":["Use type switches for error extraction to surface the real type on mismatch.","Verify argument indices after method signature changes.","Wrap custom result types so they expose Err() error."],"tags":["mock","panic","arguments","type-assertion","error"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}