-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
83 lines (76 loc) · 2.79 KB
/
Copy patherrors.go
File metadata and controls
83 lines (76 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2026 PolitePixels Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This project stands against fascism, authoritarianism, and all forms of
// oppression. We built this to empower people, not to enable those who would
// strip others of their rights and dignity.
package piko
import (
"piko.sh/piko/internal/daemon/daemon_dto"
"piko.sh/piko/internal/safeerror"
)
// Error is an error that carries a user-safe message separate from its internal cause.
//
// In production, only SafeMessage() reaches the user; the full error detail is logged
// server-side. In development mode (dev or dev-i), the full error string is shown for
// easier debugging.
//
// Any error in the chain can implement Error; the error boundary will discover it via
// errors.As. Existing sentinels and errors.Is chains are preserved through Unwrap().
type Error = safeerror.Error
var (
// NewError wraps a cause error with a user-safe message. The cause's Error() string is
// used for internal logging, while safeMessage is the string shown to users in
// production.
//
// The returned error implements Unwrap(), so errors.Is and errors.As continue to work
// through the chain.
//
// Example:
//
// return piko.NewError("something went wrong", err)
NewError = safeerror.NewError
// Errorf creates an error with a user-safe message and a formatted internal cause (using
// fmt.Errorf semantics for the internal part).
//
// Example:
//
// return piko.Errorf("could not process order",
// "loading order %s from database: %w", orderID, err)
Errorf = safeerror.Errorf
)
// IsDevelopmentMode reports whether the current request is being served under the dev or
// dev-i runtime modes.
//
// Use this in error page Render functions to decide whether to show internal error
// details. Returns false when r is nil or the request context does not carry runtime mode
// information.
//
// Takes r (*RequestData) which provides the request context to check.
//
// Returns bool which is true when the request is being served under the dev or dev-i
// runtime modes.
func IsDevelopmentMode(r *RequestData) bool {
if r == nil {
return false
}
ctx := r.Context()
if ctx == nil {
return false
}
pctx := daemon_dto.PikoRequestCtxFromContext(ctx)
if pctx == nil {
return false
}
return pctx.DevelopmentMode
}