-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patherrors.go
486 lines (458 loc) · 12.5 KB
/
errors.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2021 Airbus Defence and Space
//
// 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.
package godal
import (
"bytes"
"errors"
"sync"
)
var errorHandlerMu sync.Mutex
var errorHandlerIndex int
// ErrorHandler is a function that can be used to override godal's default behavior
// of treating all messages with severity >= CE_Warning as errors. When an ErrorHandler
// is passed as an option to a godal function, all logs/errors emitted by gdal will be passed
// to this function, which can decide wether the parameters correspond to an actual error
// or not.
//
// If the ErrorHandler returns nil, the parent function will not return an error. It is up
// to the ErrorHandler to log the message if needed.
//
// If the ErrorHandler returns an error, that error will be returned as-is to the caller
// of the parent function
type ErrorHandler func(ec ErrorCategory, code int, msg string) error
type errorHandlerWrapper struct {
fn ErrorHandler
err error
}
var errorHandlers = make(map[int]*errorHandlerWrapper)
func registerErrorHandler(fn ErrorHandler) int {
errorHandlerMu.Lock()
defer errorHandlerMu.Unlock()
for errorHandlerIndex == 0 || errorHandlers[errorHandlerIndex] != nil {
errorHandlerIndex++
}
errorHandlers[errorHandlerIndex] = &errorHandlerWrapper{fn: fn}
return errorHandlerIndex
}
func getErrorHandler(i int) *errorHandlerWrapper {
errorHandlerMu.Lock()
defer errorHandlerMu.Unlock()
return errorHandlers[i]
}
func unregisterErrorHandler(i int) {
errorHandlerMu.Lock()
defer errorHandlerMu.Unlock()
delete(errorHandlers, i)
}
type errorAndLoggingOpts struct {
eh ErrorHandler
config []string
}
type errorCallback struct {
fn ErrorHandler
}
type errorAndLoggingOption interface {
setErrorAndLoggingOpt(elo *errorAndLoggingOpts)
}
// ErrLogger is an option to override default error handling.
//
// See ErrorHandler.
func ErrLogger(fn ErrorHandler) interface {
errorAndLoggingOption
AddGeometryOption
BandCreateMaskOption
BandIOOption
BoundsOption
BufferOption
BuildOverviewsOption
BuildVRTOption
ClearOverviewsOption
CloseOption
CopyLayerOption
CreateFeatureOption
CreateLayerOption
CreateSpatialRefOption
DatasetCreateMaskOption
DatasetCreateOption
DatasetIOOption
DatasetTranslateOption
DatasetVectorTranslateOption
DatasetWarpIntoOption
DatasetWarpOption
DeleteFeatureOption
DifferenceOption
FeatureCountOption
FillBandOption
FillNoDataOption
GeoJSONOption
GeometryTransformOption
GeometryReprojectOption
GeometryWKBOption
GeometryWKTOption
GetGeoTransformOption
GMLExportOption
HistogramOption
IntersectsOption
IntersectionOption
MetadataOption
NewFeatureOption
NewGeometryOption
OpenOption
PolygonizeOption
RasterizeGeometryOption
RasterizeOption
RasterizeIntoOption
SetColorInterpOption
SetColorTableOption
SetDescriptionOption
SetGeometryOption
SetFieldValueOption
SetNoDataOption
SetScaleOffsetOption
SetGeoTransformOption
SetGeometryColumnNameOption
SetProjectionOption
SetSpatialRefOption
SieveFilterOption
SimplifyOption
SpatialRefValidateOption
SubGeometryOption
TransformOption
UnionOption
UpdateFeatureOption
VSIHandlerOption
VSIOpenOption
VSIUnlinkOption
WKTExportOption
StatisticsOption
SetStatisticsOption
ClearStatisticsOption
GridOption
NearblackOption
DemOption
SetGCPsOption
GCPsToGeoTransformOption
RegisterPluginOption
ExecuteSQLOption
StartTransactionOption
CloseResultSetOption
RollbackTransactionOption
CommitTransactionOption
} {
return errorCallback{fn}
}
func (ec errorCallback) setErrorAndLoggingOpt(elo *errorAndLoggingOpts) {
elo.eh = ec.fn
}
func (ec errorCallback) setAddGeometryOpt(ao *addGeometryOpts) {
ao.errorHandler = ec.fn
}
func (ec errorCallback) setBandCreateMaskOpt(o *bandCreateMaskOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setBandIOOpt(o *bandIOOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setBoundsOpt(o *boundsOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setBufferOpt(o *bufferOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setBuildOverviewsOpt(o *buildOvrOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setBuildVRTOpt(o *buildVRTOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setClearOverviewsOpt(o *clearOvrOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setCloseOpt(o *closeOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setCopyLayerOpt(o *copyLayerOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setCreateFeatureOpt(cfo *createFeatureOpts) {
cfo.errorHandler = ec.fn
}
func (ec errorCallback) setCreateLayerOpt(o *createLayerOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setCreateSpatialRefOpt(o *createSpatialRefOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetCreateMaskOpt(o *dsCreateMaskOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetCreateOpt(o *dsCreateOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetIOOpt(o *datasetIOOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetTranslateOpt(o *dsTranslateOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetVectorTranslateOpt(o *dsVectorTranslateOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetWarpIntoOpt(o *dsWarpIntoOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDatasetWarpOpt(o *dsWarpOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDeleteFeatureOpt(o *deleteFeatureOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDifferenceOpt(do *differenceOpts) {
do.errorHandler = ec.fn
}
func (ec errorCallback) setFeatureCountOpt(o *featureCountOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setFillBandOpt(o *fillBandOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setFillnodataOpt(o *fillnodataOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeojsonOpt(o *geojsonOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryColumnNameOpt(o *setGeometryColumnNameOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryTransformOpt(o *geometryTransformOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryReprojectOpt(o *geometryReprojectOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryWKBOpt(o *geometryWKBOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryWKTOpt(o *geometryWKTOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGetGeoTransformOpt(o *getGeoTransformOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGMLExportOpt(o *gmlExportOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setHistogramOpt(o *histogramOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setIntersectsOpt(o *intersectsOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setIntersectionOpt(o *intersectionOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setMetadataOpt(o *metadataOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDescriptionOpt(o *setDescriptionOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setNewFeatureOpt(o *newFeatureOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setNewGeometryOpt(o *newGeometryOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setOpenOpt(oo *openOpts) {
oo.errorHandler = ec.fn
}
func (ec errorCallback) setPolygonizeOpt(o *polygonizeOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRasterizeGeometryOpt(o *rasterizeGeometryOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRasterizeOpt(o *rasterizeOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRasterizeIntoOpt(o *rasterizeIntoOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetColorInterpOpt(ndo *setColorInterpOpts) {
ndo.errorHandler = ec.fn
}
func (ec errorCallback) setSetColorTableOpt(ndo *setColorTableOpts) {
ndo.errorHandler = ec.fn
}
func (ec errorCallback) setSetGeometryOpt(o *setGeometryOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetFieldValueOpt(o *setFieldValueOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetGeoTransformOpt(o *setGeoTransformOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetProjectionOpt(o *setProjectionOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetNoDataOpt(ndo *setNodataOpts) {
ndo.errorHandler = ec.fn
}
func (ec errorCallback) setSetScaleOffsetOpt(soo *setScaleOffsetOpts) {
soo.errorHandler = ec.fn
}
func (ec errorCallback) setSetSpatialRefOpt(ndo *setSpatialRefOpts) {
ndo.errorHandler = ec.fn
}
func (ec errorCallback) setSieveFilterOpt(sfo *sieveFilterOpts) {
sfo.errorHandler = ec.fn
}
func (ec errorCallback) setSimplifyOpt(o *simplifyOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSpatialRefValidateOpt(o *spatialRefValidateOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSubGeometryOpt(so *subGeometryOpts) {
so.errorHandler = ec.fn
}
func (ec errorCallback) setTransformOpt(o *trnOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setUnionOpt(uo *unionOpts) {
uo.errorHandler = ec.fn
}
func (ec errorCallback) setUpdateFeatureOpt(o *updateFeatureOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setVSIHandlerOpt(o *vsiHandlerOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setVSIOpenOpt(o *vsiOpenOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setVSIUnlinkOpt(o *vsiUnlinkOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setWKTExportOpt(o *srWKTOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setStatisticsOpt(o *statisticsOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetStatisticsOpt(o *setStatisticsOpt) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setClearStatisticsOpt(o *clearStatisticsOpt) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGridCreateOpt(o *gridCreateOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGridOpt(o *gridOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setNearblackOpt(o *nearBlackOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setDemOpt(o *demOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setSetGCPsOpt(o *setGCPsOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGCPsToGeoTransformOpts(o *gcpsToGeoTransformOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRegisterPluginOpt(o *registerPluginOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setExecuteSQLOpt(o *executeSQLOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setReleaseResultSetOpt(o *closeResultSetOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setStartTransactionOpt(o *startTransactionOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRollbackTransactionOpt(o *rollbackTransactionOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setCommitTransactionOpt(o *commitTransactionOpts) {
o.errorHandler = ec.fn
}
type multiError struct {
errs []error
}
// Error is the standard error interface
func (me *multiError) Error() string {
w := bytes.NewBufferString(me.errs[0].Error())
for i := 1; i < len(me.errs); i++ {
w.WriteByte('\n')
w.WriteString(me.errs[i].Error())
}
return w.String()
}
// As is the standard error wrapping interface
func (me *multiError) As(target interface{}) bool {
for _, err := range me.errs {
if errors.As(err, target) {
return true
}
}
return false
}
// Is is the standard error wrapping interface
func (me *multiError) Is(target error) bool {
for _, err := range me.errs {
if errors.Is(err, target) {
return true
}
}
return false
}
func combine(e1, e2 error) error {
switch {
case e1 == nil:
return e2
case e2 == nil:
return e1
}
if me1, ok := e1.(*multiError); ok {
if me2, ok := e2.(*multiError); ok {
me1.errs = append(me1.errs, me2.errs...)
} else {
me1.errs = append(me1.errs, e2)
}
return me1
} else if me2, ok := e2.(*multiError); ok {
me := &multiError{}
me.errs = make([]error, 1, len(me2.errs)+1)
me.errs[0] = e1
me.errs = append(me.errs, me2.errs...)
return me
} else {
return &multiError{errs: []error{e1, e2}}
}
}
var SkipWarnings = ErrLogger(
func(ec ErrorCategory, code int, message string) error {
if ec > CE_Warning {
return errors.New(message)
}
return nil
})