<commit_before>package middlewares

import (
	"github.com/cozy/cozy-stack/pkg/instance"
	"github.com/labstack/echo"
)

// NeedInstance is an echo middleware which will display an error
// if there is no instance.
func NeedInstance(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		if c.Get("instance") != nil {
			return next(c)
		}
		i, err := instance.Get(c.Request().Host)
		if err != nil {
			return err
		}
		c.Set("instance", i)
		return next(c)
	}
}

// GetInstance will return the instance linked to the given echo
// context or panic if none exists
func GetInstance(c echo.Context) *instance.Instance {
	return c.Get("instance").(*instance.Instance)
}<commit_msg>Do not send a jsonapi error for instance fetching errors<commit_after>package middlewares

import (
	"net/http"

	"github.com/cozy/cozy-stack/pkg/instance"
	"github.com/labstack/echo"
)

// NeedInstance is an echo middleware which will display an error
// if there is no instance.
func NeedInstance(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		if c.Get("instance") != nil {
			return next(c)
		}
		i, err := instance.Get(c.Request().Host)
		if err != nil {
			switch err {
			case instance.ErrNotFound:
				return echo.NewHTTPError(http.StatusNotFound, err)
			case instance.ErrIllegalDomain:
				return echo.NewHTTPError(http.StatusBadRequest, err)
			default:
				return echo.NewHTTPError(http.StatusInternalServerError, err)
			}
		}
		c.Set("instance", i)
		return next(c)
	}
}

// GetInstance will return the instance linked to the given echo
// context or panic if none exists
func GetInstance(c echo.Context) *instance.Instance {
	return c.Get("instance").(*instance.Instance)
}
{'fraction_non_alphanumeric': 0.11658932714617169, 'fraction_numerical': 0.0, 'mean_word_length': 3.9285714285714284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5081091', 'n_tokens_mistral': 590, 'n_tokens_neox': 554, 'n_words': 158}