Dear George,

thank you for the rivet tries.

About the bug, as usual, please file a bug report.

About the error return. Here is my opinion:

You are interested in doing your own return page.
Why ? It depends on the requested format.
I follow the book "RESTful Web Services Cookbook" by Subbu Allamaraju.
Half the book is about "error return"...

My example:

- a data base is queried and there are the following formats available:
  - web (pretty web page)
  - http table
  - xml table
  - csv table

First, I find out the required destination format:
- A form variable
- if not given: headers(Accept)
  -> there is a new method in 2.0.5 to easily parse this

Then I look into the data base.
If the required data is not found or there is another error, I output
error pages depending on the required format and the set http status.
See also, that I flag "not found data" as cacheable, but server errors
as not ;-)

    if {[catch {
        set resultData [wwwbaseProductDataGet\
                [env REMOTE_ADDR]\
                [var get log]\
                $response(key)\
                ""\
                $response(country)\
                $response(language)\
                $response(date)\
                $response(format)]
    } Err dErr]} {
        switch -exact -- [dict get $dErr -errorcode] {
            PARAM  {
                # > Return 400 Bad request
                wwwbaseCacheSet 1
                headers numeric 400
            }
            NOT_FOUND {
                # > Return 404 Not found
                wwwbaseCacheSet 1
                headers numeric 404
            }
            default {
                # > Return 500 Internal server error
                wwwbaseCacheSet 0
                headers numeric 500
            }
        }
        switch -exact -- $response(format) {
            csv {
                puts "Error\n$Err"
            }
            json {
                puts "\{error: \"$Err\"\}"
            }
            xml {
                puts "<error>$Err</error>"
            }
            html {
                puts "<html><head><title>\
                        [mce PageTitleProductRequest]</title>\
                        <body><h1>[mce ErrSearch $Err]</h1></body>"
            }
            form {
                switch -exact -- [dict get $dErr -errorcode] {
                    PARAM  {
                        puts [mce ErrParam $Err]
                    }
                    NOT_FOUND {
                        puts [mce ErrSearch $Err]
                    }
                    default {
                        puts [mce ErrServer $Err]
                    }
                }
                # puts $Err<br/>[string map {\n <br/>} $::errorInfo]
            }
        }
    } else {
        # > Correct response is cacheable
        wwwbaseCacheSet 1
        switch -exact -- $response(format) {
            csv {
                headers set Content-Disposition\
                        "attachment; filename=product.csv"
            }
            form {
                puts ""
            }
        }
        puts -nonewline $resultData
        
    }

In addition, I have added header information, if the result is cacheable
or not:
proc ::wwwbase::wwwbaseCacheSet {{fCacheable 1}} {
    if {$fCacheable} {
                headers set Cache-Control "max-age=86400,must-revalidate"
        } else {
                headers set Cache-Control "no-cache,no-store"
        }
}

My 2 pennies,
Harald

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to