How to terminate request in pre-process and return custom error response


#1

I am validating a field in the payload using a pre-process script and it works as below.

if (event.request.payload) {  // use 'payload' for request
    if (!event.request.payload.email) {
            throw 'Email field is required';
        }
        else {
            // validate email address
		    var result = platform.api.get("mailboxlayer?email=" + event.request.payload.email);
		    var_dump(result);
		    
		    if(result.error){
		        throw 'Email verification error!';
		    }
		    if (!(result.format_valid && result.smtp_check)){
		       throw 'Email validation failed';
		    }
        }

}

I however want to return an error object with details of the error instead of just throwing an error string. I can see a null context in the response, can I add the details of the error to this property and if so, how?

Any idea how to go about this will be greatly appreciated.