;; Copyright 2000-2005 Ryan Culpepper
;; Released under the terms of the modified BSD license (see the file
;; COPYRIGHT for terms).

(module exceptions mzscheme
  (provide (struct exn:spgsql ())
           (struct exn:spgsql:fatal ())
           (struct exn:spgsql:nonfatal ())
           (struct exn:spgsql:auth (type))
           (struct exn:spgsql:communication (type))
           (struct exn:spgsql:internal (location))
           (struct exn:spgsql:query ())
           (struct exn:spgsql:user (type))
  ;; exn:spgsql
  ;; The base of all errors generated by the spgsql library.
  (define-struct (exn:spgsql exn:fail) ())
  ;; exn:spgsql:fatal
  ;; A fatal error occurred. The backend link should be disconnected.
  ;; Query processing may or may not be affected.
  (define-struct (exn:spgsql:fatal exn:spgsql) ())
  ;; exn:spgsql:nonfatal
  ;; A non-fatal error has occurred. The backend link should be in a consistent
  ;; state, ready to accept queries.
  (define-struct (exn:spgsql:nonfatal exn:spgsql) ())
  ;; exn:spgsql:auth
  ;; An error occurred in the authentication step of connection. The connection
  ;; has not been made, but may be completed successfully with different
  ;; parameters.
  (define-struct (exn:spgsql:auth exn:spgsql:fatal) (type))
  ;; exn:spgsql:communication
  ;; There was a problem communicating with the server; either the server could
  ;; not be found, or the connection was unexpectedly disconnected.
  (define-struct (exn:spgsql:communication exn:spgsql:fatal) (type))
  ;; exn:spgsql:internal
  ;; There was an internal error in the spgsql code which may have upset the
  ;; integrity of the connection. Please report the conditions of the error.
  (define-struct (exn:spgsql:internal exn:spgsql:fatal) (location))
  ;; exn:spgsql:query
  ;; The backend sent an ErrorResponse in response to a user-submitted query.
  ;; These include SQL syntax errors (PARSE), field/table/etc not found, ...
  (define-struct (exn:spgsql:query exn:spgsql:nonfatal) ())
  ;; exn:spgsql:user
  ;; Invalid information was supplied to the spgsql library code. This includes
  ;; arity mismatches for user-supplied fold functions, executing a query which
  ;; returns multiple results with query-value (which expects a single value),
  ;; attempting to use a connection that is blocked on COPY IN data, etc.
  (define-struct (exn:spgsql:user exn:spgsql:nonfatal) (type))
  (define-syntax define-raise
    (syntax-rules ()
      [(_ name constructor)
       (define (name format-string . format-args)
         (raise (constructor (string->immutable-string
                              (apply format format-string format-args))
  (define-syntax define-raise/sym
    (syntax-rules ()
      [(_ name constructor)
       (define (name extra-symbol format-string . format-args)
         (raise (constructor (string->immutable-string
                              (apply format format-string format-args))
  ;; raise-auth-error : symbol string -> raises exn
  (define-raise/sym raise-auth-error make-exn:spgsql:auth)
  ;; raise-communication-error : symbol string -> raises exn
  (define-raise/sym raise-communication-error make-exn:spgsql:communication)
  ;; raise-internal-error : symbol string -> raises exn
  (define-raise/sym raise-internal-error make-exn:spgsql:internal)
  ;; raise-query-error : string -> raises exn
  (define-raise raise-query-error make-exn:spgsql:query)
  ;; raise-sp-user-error : symbol string -> raises exn
  (define-raise/sym raise-sp-user-error make-exn:spgsql:user)