numerus/pkg
jordi fita mas 3d3221e202 Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.

Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.

I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:

  goos: linux
  goarch: amd64
  pkg: dev.tandem.ws/tandem/numerus/pkg
  cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
  BenchmarkValidUuid-4            36946050                29.37 ns/op
  BenchmarkValidUuid_Re-4          3633169               306.70 ns/op

The regular expression used for the benchmark was:

  var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")

And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:

  "f47ac10b-58cc-0372-8567-0e02b2c3d479"

I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array.  As far as i know, that package can not
do that.

[0]: tandem/tandem#40
2023-07-17 11:46:11 +02:00
..
company.go Create validation function for SQL domains and for phones 2023-07-03 11:31:59 +02:00
contacts.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
dashboard.go Compute the total amount, base plus taxes, of all expenses 2023-07-13 20:50:26 +02:00
db.go Allow importing contacts from Holded 2023-07-03 00:05:47 +02:00
expenses.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
form.go Add reset button to filters 2023-07-16 20:56:11 +02:00
htmx.go Use HTMx to add product rows “inline” in the invoice form 2023-04-24 02:00:38 +02:00
invoices.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
locale.go Add currency_pattern to language relation 2023-02-23 12:12:33 +01:00
logger.go Reduce the amount of useless data in request’s log 2023-06-13 16:05:40 +02:00
login.go Log the remote address, and only trust localhost proxies 2023-06-13 14:21:54 +02:00
pgtypes.go Split contact relation into tax_details, phone, web, and email 2023-06-30 21:32:48 +02:00
products.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
profile.go Create constants for the HTMX request and response headers used 2023-04-06 12:07:20 +02:00
quote.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
recover.go Gofmt recover.go 2023-01-22 20:37:34 +01:00
router.go Compute the total amount, base plus taxes, of all expenses 2023-07-13 20:50:26 +02:00
slug.go Add the customer name to the invoice’s PDF file name 2023-07-07 11:34:34 +02:00
template.go Add the customer name to the invoice’s PDF file name 2023-07-07 11:34:34 +02:00
uuid.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00
uuid_test.go Return HTTP 404 instead of 500 for invalid UUID values in URL 2023-07-17 11:46:11 +02:00