จนถึงตอนนี้ เราได้เห็น function “shape” สองแบบใน pipeline ของเรา คือ function แบบ one-track และ function แบบ “switch” แต่แน่นอนว่าเราอาจต้องทำงานกับ function แบบอื่นๆ อีกมากมาย มาดูอีกสองแบบกันตอนนี้เลย:
- function ที่โยน exception
- function แบบ “dead-end” ที่ไม่คืนอะไรกลับมาเลย
Handling Exceptions (การจัดการ Exceptions)
เราหลีกเลี่ยงการโยน exception ใน code ของเรา เอง แต่แล้ว exception ที่เกิดขึ้นใน code ที่เราควบคุมไม่ได้ เช่นใน library หรือ service ล่ะ ก่อนหน้านี้เราแนะนำว่า exception ส่วนใหญ่ไม่ได้เป็นส่วนหนึ่งของการออกแบบ domain และไม่จำเป็นต้องถูกดักจับ ยกเว้นที่ระดับบนสุด แต่ถ้าเรา ต้องการ จริงๆ ที่จะให้ exception เป็นส่วนหนึ่งของ domain ล่ะ เราจะทำแบบนั้นได้อย่างไร
วิธีแก้ก็ตรงไปตรงมา—เราแค่สร้าง function “adapter block” อีกตัวหนึ่ง ที่แปลง function ที่โยน exception ให้กลายเป็น function ที่คืนค่า Result ตามที่แสดงใน figure .
ตัวอย่างเช่น สมมติว่าเราต้องการดักจับ timeout จาก service ระยะไกล แล้วแปลงให้เป็น RemoteServiceError เราจะต้องทำงานกับ service หลายตัว ดังนั้นมาเริ่มด้วยการนิยาม ServiceInfo เพื่อเก็บข้อมูล service ที่ทำให้เกิด error กันก่อน:
| | type ServiceInfo = { |
| | Name : string |
| | Endpoint: Uri |
| | } |
แล้วเราก็สามารถนิยาม error type ที่ต่อยอดจากสิ่งนี้ได้:
| | type RemoteServiceError = { |
| | Service : ServiceInfo |
| | Exception : System.Exception |
| | } |
เราส่ง service info กับ function service ดั้งเดิมเข้าไปใน adapter block ที่ดักจับ exception บางส่วน และคืน Result ออกมาในกรณีนั้นๆ นี่คือตัวอย่างเมื่อ function service รับ parameter ตัวเดียว ( x ใน code ด้านล่าง):
| | /// "Adapter block" that converts exception-throwing services |
| | /// into Result-returning services. |
| | let serviceExceptionAdapter serviceInfo serviceFn x = |
| | try |
| | // call the service and return success |
| | Ok (serviceFn x) |
| | with |
| | | :? TimeoutException as ex -> |
| | Error {Service=serviceInfo; Exception=ex} |
| | | :? AuthorizationException as ex -> |
| | Error {Service=serviceInfo; Exception=ex} |
สังเกตว่าเราไม่ได้ดักจับ exception ทุกแบบ ที่เป็นไปได้ เฉพาะแบบที่เกี่ยวข้องกับ domain เท่านั้น
ถ้า function service มี parameter สองตัว เราต้องนิยาม adapter อีกตัวเพื่อรองรับกรณีนั้น และเป็นแบบนี้ไปเรื่อยๆ
| | let serviceExceptionAdapter2 serviceInfo serviceFn x y = |
| | try |
| | Ok (serviceFn x y) |
| | with |
| | | :? TimeoutException as ex -> ... |
| | | :? AuthorizationException as ex -> ... |
Adapter block เหล่านี้เป็นแบบ generic ที่ปรับใช้กับ function ไหนก็ได้ ในบางกรณีคุณอาจอยากได้ custom adapter block เฉพาะสำหรับ service หนึ่งๆ เช่น ตัวที่แปลง exception จาก database ให้เป็น choice type DatabaseError ที่มี case ที่เป็นมิตรกับ domain อย่าง “record not found” และ “duplicate key”
ทีนี้เพื่อจะใช้ adapter นี้ เราสร้าง ServiceInfo แล้วส่ง function service เข้าไป ตัวอย่างเช่น ถ้า function service คือ function ตรวจสอบที่อยู่ code ก็จะมีหน้าตาประมาณนี้:
| | let serviceInfo = { |
| | Name = "AddressCheckingService" |
| | Endpoint = ... |
| | } |
| | |
| | // exception-throwing service |
| | let checkAddressExists address = |
| | ... |
| | |
| | // Result-returning service |
| | let checkAddressExistsR address = |
| | // adapt the service |
| | let adaptedService = |
| | serviceExceptionAdapter serviceInfo checkAddressExists |
| | // call the service |
| | adaptedService address |
เพื่อให้ชัดเจนว่า function ใหม่นี้เป็นเวอร์ชันที่คืนค่า Result เราจะตั้งชื่อมันว่า checkAddressExistsR โดยมี R ต่อท้าย (ใน code จริงๆ คุณอาจแค่ตั้งชื่อเดียวกับ function ดั้งเดิม—แบบ “shadowing” มันไปเลย)
มาตรวจ signature กันอีกครั้งเพื่อให้แน่ใจว่าเราได้สิ่งที่ต้องการ function ดั้งเดิมระบุว่ามันคืนค่า CheckedAddress เสมอ:
| | checkAddressExists : |
| | UnvalidatedAddress -> CheckedAddress |
แต่เรารู้แล้วว่า signature นี้ลวงตา ถ้าเราดู signature ของ function เวอร์ชัน “adapted” ใหม่ เราจะเห็นว่ามันสื่อ ความหมายได้ชัดเจนกว่ามาก มันระบุว่าอาจล้มเหลวและคืน error ออกมาได้
| | checkAddressExistsR : |
| | UnvalidatedAddress -> Result<CheckedAddress,RemoteServiceError> |
Error type คือ RemoteServiceError ดังนั้นถ้าเราต้องการใช้ function นี้ใน pipeline ของเรา เราจะต้องเพิ่ม case สำหรับ remote error เข้าไปใน type PlaceOrderError ของเรา:
| | type PlaceOrderError = |
| | | Validation of ValidationError |
| | | Pricing of PricingError |
| | | RemoteService of RemoteServiceError // new! |
จากนั้นเราต้องแปลง RemoteServiceError ให้กลายเป็น PlaceOrderError ร่วม เมื่อสร้าง function เวอร์ชัน R เหมือนกับที่เราทำก่อนหน้านี้:
| | let checkAddressExistsR address = |
| | // adapt the service |
| | let adaptedService = |
| | serviceExceptionAdapter serviceInfo checkAddressExists |
| | // call the service |
| | address |
| | |> adaptedService |
| | |> Result.mapError RemoteService // lift to PlaceOrderError |
Handling Dead-End Functions (การจัดการฟังก์ชันแบบ Dead-End)
function อีก type ของ function ที่พบได้บ่อยคือสิ่งที่เรียกว่า function แบบ “dead-end” หรือ “fire-and-forget” นั่นคือ function ที่รับ input เข้ามาแต่ไม่คืน output อะไรออกมาเลย
function แบบนี้ส่วนใหญ่จะเป็นการเขียนลง I/O บางอย่าง ตัวอย่างเช่น function logging ด้านล่างนี้ไม่มี output เลย:
| | // string -> unit |
| | let logError msg = |
| | printfn "ERROR %s" msg |
ตัวอย่างอื่นๆ ได้แก่การเขียนลง database การ post เข้า queue และอื่นๆ
เพื่อให้ function แบบ dead-end ใช้งานกับ pipeline แบบ two-track ได้ เราจะต้องใช้ adapter block อีกตัวหนึ่ง ในการสร้าง มันขึ้นมา ก่อนอื่นเราต้องมีวิธีเรียก function dead-end ด้วย input แล้วคืนค่า input ดั้งเดิมกลับมา—function แบบ “pass-through” มาเรียก function นี้ว่า tee กัน:
นี่คือ code:
| | // ('a -> unit) -> ('a -> 'a) |
| | let tee f x = |
| | f x |
| | x |
Signature นี้แสดงให้เห็นว่ามันรับ function ที่คืนค่า unit แบบไหนก็ได้ และปล่อย function แบบ one-track ออกมา
จากนั้นเราสามารถใช้ Result.map แปลง output ของ tee ให้เป็น function แบบ two-track ได้:
| | // ('a -> unit) -> (Result<'a,'error> -> Result<'a,'error>) |
| | let adaptDeadEnd f = |
| | Result.map (tee f) |
ตอนนี้เราก็มีวิธีนำ function แบบ dead-end อย่าง logError มาแปลงให้เป็น function แบบ two-track ที่เสียบเข้าไปใน pipeline ของเราได้แล้ว