"""Format error response.""" from abacus_common_data.currency import Currency from owsresponse import response from payment.constants.error import ERROR_UNKNOWN_CURRENCY def validate_currency(currency_code, raise_on_error=False): """Validate whether given currency_code is valid.""" try: Currency(currency_code) except KeyError: if raise_on_error: raise ValueError(ERROR_UNKNOWN_CURRENCY.format(code=currency_code)) return validation_error(ERROR_UNKNOWN_CURRENCY.format(code=currency_code)) def validation_error(flat_error_message=None, status_code=400, **kwargs): """Format error response.""" message = flat_error_message or kwargs return response.create_error_response( code='error', message=message, status=status_code )