c# - What HttpStatusCode and ReasonPhase in Web API to return for defensive programming? -
when writing public function, follow practices of defensive programming, this.
public long createsection(section section) { if (section == null) throw new argumentnullexception("section"); var entityid = section.entityid; if (entityid == 0) throw new argumentexception("to add section, entity must have been saved entityid", "section"); debug.assert(section.id == 0, "if give such section, don't minid save it."); ... return section.id; }
i going wrap asp.net web api 2.x function. upon argumentexceptions, think need throw httpresponseexception httpstatuscode , reasonphase.
- what httpstatuscode should use?
- should overwrite default reasonphase value argument name missing?
- what best practice handle exceptions thrown in api controller function , tell clients?
as per personal oppinion,
- i use 400 bad request. 500 should avoided cause should handle exceptions , isn't complete breakdown.
- yes, it's let consumer know did wrong may correct next request. if service went down in shower of sparks, how know did wrong?
- do not throw them. if do, make sure handle them output decent the
application_error
inglobal.asax
in old asp projects. clean 500 breakdown isn't pretty after :)
Comments
Post a Comment