helper

package
v1.0.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const MimeTypeNOSNIFF = "nosniff"

MimeTypeNOSNIFF — значение заголовка X-Content-Type-Options для предотвращения MIME-сниффинга.

Variables

This section is empty.

Functions

func JSONError

func JSONError(w http.ResponseWriter, err *Error, code int)

JSONError отправляет ошибку в формате JSON. Устанавливает заголовки Content-Type и X-Content-Type-Options.

Пример:

helper.JSONError(w, helper.NewError("user not found"), http.StatusNotFound)

func WriteError

func WriteError(w http.ResponseWriter, r *http.Request, err string, code int)

WriteError отправляет ошибку в формате JSON или plain text в зависимости от заголовков Accept и Content-Type запроса.

Если клиент ожидает JSON (application/json), отправляет JSON-ответ. Иначе отправляет plain text через http.Error.

Пример:

helper.WriteError(w, r, "user not found", http.StatusNotFound)

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error — структура ошибки для JSON-ответов.

func NewError

func NewError(error string) *Error

NewError создаёт новую ошибку.

func (*Error) Error

func (e *Error) Error() string

Error возвращает сообщение об ошибке. Реализует интерфейс error.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON сериализует ошибку в JSON. Формат: {"error": "сообщение"}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL