kataras/iris · error

struct: method: %s does not exist

Error message

struct: method: %s does not exist

What it means

Struct.MethodHandler panics when the requested methodName does not exist as an exported method on the registered struct's concrete type (hero/struct.go:185). It uses reflect Type.MethodByName, so only exported methods with a method-value receiver are found.

Source

Thrown at hero/struct.go:185

				} // #1629
			}

			elem.FieldByIndex(b.Input.StructFieldIndex).Set(input)
		}
	}

	return ctrl, nil
}

// MethodHandler accepts a "methodName" that should be a valid an exported
// method of the struct and returns its converted Handler.
//
// Second input is optional,
// even zero is a valid value and can resolve path parameters correctly if from root party.
func (s *Struct) MethodHandler(methodName string, paramsCount int) context.Handler {
	m, ok := s.ptrValue.Type().MethodByName(methodName)
	if !ok {
		panic(fmt.Sprintf("struct: method: %s does not exist", methodName))
	}

	return makeHandler(m.Func, s.Container, paramsCount)
}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Verify the method name spelling and that it is exported (starts with an uppercase letter).
  2. Ensure the method is defined on the exact type registered with the hero Struct, not only on an embedded or interface type.
  3. Use route registration by method reference (e.g. app.Get("/", c.Handler)) so the compiler catches missing methods instead of string lookup.
  4. Add a test that builds all MethodHandler registrations at startup so the panic surfaces early.

Example fix

// before
h := s.MethodHandler("Indexx", 0) // typo

// after
h := s.MethodHandler("Index", 0)
Defensive patterns

Strategy: validation

Validate before calling

// Check the method exists before requesting a handler
if _, ok := reflect.TypeOf(MyController{}).MethodByName("Index"); !ok {
    log.Fatal("controller method Index not found")
}
h := s.MethodHandler("Index", 0)

Type guard

func hasMethod(obj interface{}, name string) bool {
    return reflect.ValueOf(obj).MethodByName(name).IsValid()
}

Prevention

When it happens

Trigger: Calling s.MethodHandler("Name", n) where Name is misspelled, unexported, defined on an embedded/unrelated type, or the handler was built from an interface type that lacks the method.

Common situations: Refactoring renames a controller method without updating the manual MethodHandler registration; registering handlers on a struct via routes that reference methods with different casing.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/7ceccf676e169618. Report an issue: GitHub.