package web

import (
	"fmt"
	"io"
	"runtime/debug"
)

type HandleFunc func(*State, *Reply) error

type Handler interface {
	Handle(*State, *Reply) error
}

type Wrapper interface {
	Wrap(*State, *Reply) error
}

type Router interface {
	Find(string) ([]Wrapper, HandleFunc)
}

type Renderer interface {
	Render(io.Writer, string, interface{}) error
}

type panicErr struct {
	ErrMsg string
	Trace  []byte
}

func (s panicErr) Error() string {
	return fmt.Sprintf("%s\n%s", s.ErrMsg, s.Trace)
}

func recoverErr(perr *error) {
	value := recover()
	if value != nil {
		*perr = panicErr{
			ErrMsg: fmt.Sprintf("%s", value),
			Trace:  debug.Stack(),
		}
	}
}
