Should I use Try as return type in Scala services? -


i'm creating services in scala in java:

trait personservice {   def getbyid(id: long): person    def getall: iterable[person] } 

and have corresponding implementation of service.

actually service interacts db layer , business logic. methods can throw exceptions.

so have question: should wrap return type of methods of service try?

i.e. should use following declaration:

trait personservice {   def getbyid(id: long): try[person]    def getall: try[iterable[person]] } 

it depends on whether error produced service meaningful consumer. i.e. expected know different db failures , potentially retry? or mapping exceptions consumer meaningful instance? in both of cases, use try[person]

if end logging error , trying avoid, i'd recommend logging in personservice , returning option[persons].

alternatively, if want communicate information distinguish failure empty not rise level of exception, consider using either[failurereason,person]


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -