robotpony/Presto — pull request #57 by ngallagher87 (2013-07-16 21:07:40+00:00)
Title: Lint $ctx on fail

## Problem

I had a case where the `$ctx` object wouldn't correctly run through `json_encode()`:

`` tag not formatting correctly:

![](https://www.evernote.com/shard/s229/sh/5e114c11-9832-4b3f-ba12-31c8437252c7/2d22f23d62e14dbafb04d136106c4c34/deep/0/http://app.rubrix.test%20-%20Account.png)

In fact, in the raw response the chrome debugger would stop rendering the response entierly:

![](https://www.evernote.com/shard/s229/sh/20c8267a-a83e-4416-8e6d-d277d5e5b0d1/83d1a7053a9d38fb66ce3102aa2a519e/deep/0/http://app.rubrix.test%20-%20Account.png)

## Reason

This was a fail in an API that fetches data from twitter. The HTML wasn't encoding correctly to JSON, and thus was returning invalid json.

## Solution

To fix this I made a little recursive function that will apply some linting to every variable in `$ctx`. This just encodes any html entities so it's safe to send, and removes any resources (since resources cannot be encoded in json).

After running through the linter, presto now returns the following success in the previous error:

**Fixed case:**

"source":"Twitter for Mac<\/a>"

Doing this recursively made it very easy to apply some basic linting to every value in the `$ctx` object. Note that the `htmlentity()` flags are needed to ensure the validity of the json object, and json only supports `utf-8` so I also enforce that requirement.