research-pal / backend

notes taking chrome extension with backend written in Go. Helps saving the retrieving notes for your favorite webpages very handy, right there.
3 stars 0 forks source link

research the usage of new error erapping that is added after Go 1.13 #22

Closed muly closed 4 years ago

muly commented 4 years ago

ref: https://blog.golang.org/go1.13-errors

what to do: using the error wrapping %w feature of Go 1.13, achieve the below requirement. 1) create a function that can return 3 different errors (like id not found, db connection failed, invalid data) based on the input 2) call this function in another calling function, and write code in if err != nil{} block to identify the type of the error, and respond correctly. example. if error is "id not found" type print 404, if error is "db connection failed" type, print 500, if error is of "invalid data" type, print 400

pk80 commented 4 years ago

So far resolved and created PR :

  1. PUT returns StatusBadRequest if the key fields in the request are different than in the existing record in db
  2. HandleNotesGetByID resolved by using error wrapping to compare the error value and set status code
  3. HandleNotesDelete resolved by using wrapper, if the error is of ErrorNotFound, then return 400 status code
pk80 commented 4 years ago
  1. Removed go-common dependency,
  2. Tested all methods, and
  3. Documented the tests in https://docs.google.com/document/d/1vZh-zVL7TmFgznS2ptK2aHbqitnNQP6m7Y6U8BPIq-8/edit#heading=h.m9rixz5kvx0i
  4. Please let me know for changes.
  5. Otherwise good to merge.