#1 2014-04-28 13:01:25

LarsS
Member
Registered: 2014-04-25
Posts: 4

Non-framework error/result handling with services as methods

Hi,

I have been looking a lot at the mORMot documentation lately as well as making some demo projects. I think it is a great framework! and have started to use it in one or our projects.

I am focusing on the "services as methods" side and not the ORM/DB. I will need to create 3-6 "services" interfaces and each of these will have from 10-50 methods.

I am trying to figure out a good common error/result reporting "mechanism" to use consistently for all these methods.
By error/result reporting I mean how you return information about the method call to the caller. Not errors in the framework, connection etc.
e.g. a method like

  procedure AddProduct(aID: string; aName: string; aType: Integer);

could result in errors like:
- Invalid ID/Name/Type
- Product already exists
- Not allowed

My current idea is to use a "generic" record for all methods. Something like

type
  TCallResult = packed record
    Error: Integer
    Severity: TSeverityEnum;
    Text: string;
  end;

  procedure AddProduct(aID: string; aName: string; aType: Integer; out Result: TCallResult);

  or

  function AddProduct(aID: string; aName: string; aType: Integer): TCallResult;

- Is this a "good" way to implement it or is there a better way (maybe part of the framework)?
- Is using the "out" parameter better/worse than using a function with the record as the result?

br Lars

Offline

#2 2014-04-28 14:08:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,181
Website

Re: Non-framework error/result handling with services as methods

You can do this.
It perfectly does make sense.

There is no difference by using an "out" or a function result, for records: in fact, a function result is implemented like a last "out" parameter.

By default, a record will be serialized as text.
A somewhat better may be to use text-based JSON serialization, to have the TCallResult serialized as plain JSON, so will be much able to be consumed with any client (e.g. AJAX).

Thanks for the feedback.

Offline

Board footer

Powered by FluxBB