Ejemplo n.º 1
0
func Test_Injector_InterfaceOf(t *testing.T) {
	Convey("Check interface of a type", t, func() {
		iType := inject.InterfaceOf((*SpecialString)(nil))
		So(iType.Kind(), ShouldEqual, reflect.Interface)

		iType = inject.InterfaceOf((**SpecialString)(nil))
		So(iType.Kind(), ShouldEqual, reflect.Interface)

		defer func() {
			So(recover(), ShouldNotBeNil)
		}()
		iType = inject.InterfaceOf((*testing.T)(nil))
	})
}
Ejemplo n.º 2
0
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
// While Martini is in development mode, Recovery will also output the panic as HTML.
func Recovery() Handler {
	return func(c *Context, log *log.Logger) {
		defer func() {
			if err := recover(); err != nil {
				stack := stack(3)
				log.Printf("PANIC: %s\n%s", err, stack)

				// Lookup the current responsewriter
				val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
				res := val.Interface().(http.ResponseWriter)

				// respond with panic message while in development mode
				var body []byte
				if Env == DEV {
					res.Header().Set("Content-Type", "text/html")
					body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
				}

				res.WriteHeader(http.StatusInternalServerError)
				if nil != body {
					res.Write(body)
				}
			}
		}()

		c.Next()
	}
}
Ejemplo n.º 3
0
func defaultReturnHandler() ReturnHandler {
	return func(ctx *Context, vals []reflect.Value) {
		rv := ctx.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
		resp := rv.Interface().(http.ResponseWriter)
		var respVal reflect.Value
		if len(vals) > 1 && vals[0].Kind() == reflect.Int {
			resp.WriteHeader(int(vals[0].Int()))
			respVal = vals[1]
		} else if len(vals) > 0 {
			respVal = vals[0]

			if isError(respVal) {
				err := respVal.Interface().(error)
				if err != nil {
					ctx.internalServerError(ctx, err)
				}
				return
			} else if canDeref(respVal) {
				if respVal.IsNil() {
					return // Ignore nil error
				}
			}
		}
		if canDeref(respVal) {
			respVal = respVal.Elem()
		}
		if isByteSlice(respVal) {
			resp.Write(respVal.Bytes())
		} else {
			resp.Write([]byte(respVal.String()))
		}
	}
}
Ejemplo n.º 4
0
func Test_Injector_Implementors(t *testing.T) {
	Convey("Check implementors", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		g := &Greeter{"Jeremy"}
		injector.Map(g)

		So(injector.GetVal(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), ShouldBeTrue)
	})
}
Ejemplo n.º 5
0
func Test_Injector_SetParent(t *testing.T) {
	Convey("Set parent of injector", t, func() {
		injector := inject.New()
		So(injector, ShouldNotBeNil)

		injector.MapTo("another dep", (*SpecialString)(nil))

		injector2 := inject.New()
		So(injector, ShouldNotBeNil)

		injector2.SetParent(injector)

		So(injector2.GetVal(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), ShouldBeTrue)
	})
}
Ejemplo n.º 6
0
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
// While Martini is in development mode, Recovery will also output the panic as HTML.
func Recovery() macaron.Handler {
	return func(c *macaron.Context) {
		defer func() {
			if err := recover(); err != nil {
				stack := stack(3)

				panicLogger := log.Root
				// try to get request logger
				if ctx, ok := c.Data["ctx"]; ok {
					ctxTyped := ctx.(*Context)
					panicLogger = ctxTyped.Logger
				}

				panicLogger.Error("Request error", "error", err, "stack", string(stack))

				// Lookup the current responsewriter
				val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil)))
				res := val.Interface().(http.ResponseWriter)

				// respond with panic message while in development mode
				var body []byte
				if setting.Env == setting.DEV {
					res.Header().Set("Content-Type", "text/html")
					body = []byte(fmt.Sprintf(panicHtml, err, err, stack))
				}

				res.WriteHeader(http.StatusInternalServerError)
				if nil != body {
					res.Write(body)
				}
			}
		}()

		c.Next()
	}
}