PrePAN

Sign in to PrePAN

ReturnValue Meta-data wrapper object for return values

Good

Synopsis

	use ReturnValue;


	sub do_something {
		...;
		
		return ResultValue->error(
			value       => $your_usual_error_value,
			description => 'Some longer description',
			tag         => 'short_value'
			) if $failed;
			
		return ResultValue->success(
			value       => $your_usual_return_value,
			description => 'Some longer description',
			tag         => 'short_value'
			) if $failed;
		}


	my $result = do_something();
	if( $result->is_error ) {
		...; # do error stuff
		}

	my $result = do_something_else();
	for( $result->tag ) {
		when( 'tag1' ) { ... }	
		when( 'tag2' ) { ... }	
	
		}

Description

The ReturnValue class provides a very simple wrapper around a value so you can tell if it's a success or failure without taking pains to examine the particular value. Instead of using exceptions, you inspect the class of the object you get back. Errors and successes flow through the same path.

Comments

Is it ReturnValue or ResultValue? It's not clear from the code sample.
Ah, I have to update all that stuff. It's ReturnValue but I was changing the name several times as I worked on it. Thanks,
Reuse an existing vocabulary with well-defined semantics for the error response fields.

http-problem (RFC draft from 2013) has 'problemType' instead of 'tag', 'title' and 'detail' instead of 'description': http://www.mnot.net/blog/2013/05/15/http_problem

vnd.error (submitted to ietf-types in 2012) has 'message' instead of 'description': https://github.com/blongden/vnd.error
Based on your two examples, I don't see any well-defined semantics or existing vocabulary. Your examples only deal with errors. My class deals with success reporting too.
I like this. Somethimes I use nasty hashes and I have success return values, too. Different ones. This would help.

Please sign up to post a review.