vibe.d beta banner
get vibe.d
0.7.30

Function errorDisplay

Attribute to customize how errors/exceptions are displayed.

The first template parameter takes a function that maps an exception and an optional field name to a single error type. The result of this function will then be passed as the error parameter to the method referenced by the second template parameter.

Supported types for the error parameter are bool, string, Exception, or a user defined struct. The field member, if present, will be set to null if the exception was thrown after the field validation has finished.

Prototype

errorDisplay(alias DISPLAY_METHOD)() @property;

Example

Shows the basic error message display.

void getForm(string _error = null)
{
	//render!("form.dt", _error);
}

@errorDisplay!getForm
void postForm(string name)
{
	if (name.length == 0)
		throw new Exception("Name must not be empty");
	redirect("/");
}

Example

Advanced error display including the offending form field.

struct FormError {
	// receives the original error message
	string error;
	// receives the name of the field that caused the error, if applicable
	string field;
}

void getForm(FormError _error = FormError.init)
{
	//render!("form.dt", _error);
}

// throws an error if the submitted form value is not a valid integer
@errorDisplay!getForm
void postForm(int ingeter)
{
	redirect("/");
}
Authors

Sönke Ludwig

Copyright

© 2013-2016 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.