MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / handleDownloadError

Method handleDownloadError

handlers/processing/request_methods.go:82–124  ·  view source on GitHub ↗

handleDownloadError replaces the image data with fallback image if needed

(
	err errctx.Error,
)

Source from the content-addressed store, hash-verified

80
81// handleDownloadError replaces the image data with fallback image if needed
82func (r *request) handleDownloadError(
83 err errctx.Error,
84) (imagedata.ImageData, int, errctx.Error) {
85 // If there is no fallback image configured, just return the error
86 data, headers := r.getFallbackImage()
87 if data == nil {
88 return nil, 0, err
89 }
90
91 // Just send error
92 r.Monitoring().SendError(r.req.Context(), handlers.ErrCategoryDownload, err)
93
94 // We didn't return, so we have to report error
95 if err.ShouldReport() {
96 r.ErrorReporter().Report(err, r.req)
97 }
98
99 //nolint:gosec
100 slog.Warn(
101 "Could not load image. Using fallback image",
102 "request_id", r.reqID,
103 "image_url", r.imageURL,
104 "error", err.Error(),
105 )
106
107 var statusCode int
108
109 // Set status code if needed
110 if r.config.FallbackImageHTTPCode > 0 {
111 statusCode = r.config.FallbackImageHTTPCode
112 } else {
113 statusCode = err.StatusCode()
114 }
115
116 // Fallback image should have exact FallbackImageTTL lifetime
117 headers.Del(httpheaders.Expires)
118 headers.Del(httpheaders.LastModified)
119
120 r.rw.SetOriginHeaders(headers)
121 r.rw.SetIsFallbackImage()
122
123 return data, statusCode, nil
124}
125
126// getFallbackImage returns fallback image if any
127func (r *request) getFallbackImage() (imagedata.ImageData, http.Header) {

Callers 1

executeMethod · 0.95

Calls 11

getFallbackImageMethod · 0.95
WarnMethod · 0.80
SetIsFallbackImageMethod · 0.80
SendErrorMethod · 0.65
MonitoringMethod · 0.65
ShouldReportMethod · 0.65
ReportMethod · 0.65
ErrorReporterMethod · 0.65
ErrorMethod · 0.65
StatusCodeMethod · 0.65
SetOriginHeadersMethod · 0.45

Tested by

no test coverage detected