File: errors.lisp

package info (click to toggle)
cl-zs3 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 384 kB
  • sloc: lisp: 3,695; makefile: 13
file content (237 lines) | stat: -rw-r--r-- 8,253 bytes parent folder | download | duplicates (3)
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
;;;;
;;;; Copyright (c) 2008 Zachary Beane, All Rights Reserved
;;;;
;;;; Redistribution and use in source and binary forms, with or without
;;;; modification, are permitted provided that the following conditions
;;;; are met:
;;;;
;;;;   * Redistributions of source code must retain the above copyright
;;;;     notice, this list of conditions and the following disclaimer.
;;;;
;;;;   * Redistributions in binary form must reproduce the above
;;;;     copyright notice, this list of conditions and the following
;;;;     disclaimer in the documentation and/or other materials
;;;;     provided with the distribution.
;;;;
;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;;
;;;; errors.lisp

(in-package #:zs3)

(defbinder error
  ("Error"
   ("Code" (bind :code))
   ("Message" (bind :message))
   (elements-alist :data)))

(defclass amazon-error (response)
  ((code
    :initarg :code
    :accessor code)
   (message
    :initarg :message
    :accessor message)
   (error-data
    :initarg :error-data
    :accessor error-data)))

(set-element-class "Error" 'amazon-error)

(defgeneric error-data-value (name instance)
  (:method (name (response amazon-error))
    (cdr (assoc name (error-data response) :test #'equalp))))

(defmethod specialized-initialize ((response amazon-error) source)
  (let ((bindings (xml-bind 'error source)))
    (setf (code response) (bvalue :code bindings))
    (setf (message response) (bvalue :message bindings))
    (setf (error-data response) (bvalue :data bindings))))

(defmethod specialized-initialize ((response amazon-error) (source null))
  (setf (code response) "InternalError"
        (message response) nil
        (error-data response) nil))

(defmethod print-object ((response amazon-error) stream)
  (print-unreadable-object (response stream :type t)
    (prin1 (code response) stream)))

;;; Further specializing error messages/conditions

(defun report-request-error (condition stream)
  (format stream "~A~@[: ~A~]"
          (code (request-error-response condition))
          (message (request-error-response condition))))

(define-condition request-error (error)
  ((request
    :initarg :request
    :reader request-error-request)
   (response
    :initarg :response
    :reader request-error-response)
   (data
    :initarg :data
    :reader request-error-data))
  (:report report-request-error))

(defparameter *specific-errors* (make-hash-table :test 'equalp))

(defun specific-error (amazon-code)
  (gethash amazon-code *specific-errors* 'request-error))

(defgeneric signal-specific-error (response condition-name)
  (:method (response (condition-name t))
    (error 'request-error
           :request (request response)
           :response response
           :data (error-data response))))

(defgeneric maybe-signal-error (response)
  (:method ((response t))
    t)
  (:method ((response amazon-error))
    (signal-specific-error response (specific-error (code response)))))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun error-reader-name (suffix)
    (intern (concatenate 'string (symbol-name 'request-error)
                         "-"
                         (symbol-name suffix))
            :zs3)))

(defmacro define-specific-error ((condition-name code)
                                 superclasses
                                 slots &rest options)
  (labels ((slot-name (slot)
             (first slot))
           (slot-code (slot)
             (second slot))
           (slot-keyword (slot)
             (keywordify (slot-name slot)))
           (slot-definition (slot)
             `(,(slot-name slot)
               :initarg ,(slot-keyword slot)
               :reader ,(error-reader-name (slot-name slot))))
           (slot-initializer (slot)
             (list (slot-keyword slot)
                   `(error-data-value ,(slot-code slot) response))))
    `(progn
       (setf (gethash ,code *specific-errors*) ',condition-name)
       (define-condition ,condition-name (,@superclasses request-error)
         ,(mapcar #'slot-definition slots)
         ,@options)
       (defmethod signal-specific-error ((response amazon-error)
                                         (condition-name (eql ',condition-name)))
         (error ',condition-name
                :request (request response)
                :response response
                :data (error-data response)
                ,@(mapcan #'slot-initializer slots))))))


;;; The specific errors

(define-specific-error (internal-error "InternalError") () ())

(define-specific-error (slow-down "SlowDown") () ())

(define-specific-error (no-such-bucket "NoSuchBucket") ()
  ((bucket-name "BucketName")))

(define-specific-error (no-such-key "NoSuchKey") ()
  ((key-name "Key")))

(define-specific-error (access-denied "AccessDenied") () ())

(define-specific-error (malformed-xml "MalformedXML") () ())

(define-condition redirect-error (error) ())

(define-specific-error (permanent-redirect "PermanentRedirect") (redirect-error)
  ((endpoint "Endpoint")))

(define-specific-error (temporary-redirect "TemporaryRedirect") (redirect-error)
  ((endpoint "Endpoint")))

(define-specific-error (signature-mismatch "SignatureDoesNotMatch") ()
  ((string-to-sign "StringToSign")
   (canonical-request "CanonicalRequest"))
  (:report (lambda (condition stream)
             (report-request-error condition stream)
             (format stream "You signed: ~S~%Amazon signed: ~S~%and~%~S"
                            (signed-string (request-error-request condition))
                            (request-error-string-to-sign condition)
                            (request-error-canonical-request condition)))))

(define-specific-error (precondition-failed "PreconditionFailed") ()
  ((condition "Condition")))

(define-specific-error (authorization-header-malformed
                        "AuthorizationHeaderMalformed") ()
  ((region "Region")))


(define-condition linked ()
  ((url
    :initarg :url
    :reader linked-url))
  (:report (lambda (condition stream)
             (report-request-error condition stream)
             (format stream "~&For more information, see:~%  ~A"
                            (linked-url condition)))))


(define-condition bucket-restrictions (linked)
  ()
  (:default-initargs
   :url "http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html"))

(define-specific-error (invalid-bucket-name "InvalidBucketName")
    (bucket-restrictions)
  ())

(define-specific-error (bucket-exists "BucketAlreadyExists")
    (bucket-restrictions)
  ())

(define-specific-error (too-many-buckets "TooManyBuckets")
    (bucket-restrictions)
  ())


(define-specific-error (ambiguous-grant "AmbiguousGrantByEmailAddress") (linked)
  ()
  (:default-initargs
   :url "http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTacl.html"))

(define-specific-error (bucket-not-empty "BucketNotEmpty") (linked)
  ()
  (:default-initargs
   :url "http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketDELETE.html"))

(define-specific-error (invalid-logging-target "InvalidTargetBucketForLogging")
    () ())

(define-specific-error (key-too-long "KeyTooLong") (linked)
  ()
  (:default-initargs
   :url "http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html"))

(define-specific-error (request-time-skewed "RequestTimeTooSkewed") (linked)
  ()
  (:default-initargs
   :url "http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationTimeStamp"))

(define-specific-error (operation-aborted "OperationAborted") () ())