A mock's response did not render
When a mock’s template cannot be rendered, whoever called the project gets a 500 in place of the mock’s response:
{ "type": "https://mockhub.dev/problems/mock-render-failed", "title": "Mock response did not render", "status": 500, "detail": "The mock that matched this request could not render its response. The project's request log says why."}What went wrong stays with the project. Open the request on the project’s Request logs tab: under Response, The mock’s response did not render says why in plain words, such as “Name has no method called ‘nope’.”, and Open the mock opens it in the editor. A project that does not log request or response data keeps no reason, and the log says so; Try it finds it. Mock response did not render describes the error itself.
In the mock editor, Try it shows the mock server’s own report, with the position of the expression at fault:
[ERROR] 3:13: could not find helper: 'randm' "name": "{{randm 'Name.firstName'}} ^A few mistakes leave the rest of the body intact. The mock answers with its own status, to the caller too, and the error stands where the value should be:
[ERROR: ^ is not a valid mathematical operator]Read the position
Section titled “Read the position”The position in [ERROR] 3:13 does not count from the top of the body. The mock server reads the body from its first {{, so line 1 is the line where the first expression starts, and on that line the column counts from that {{. On the lines after it, the column counts from the start of the line. Columns count from 0.
In the example above, the body starts with a line holding only {, and its first expression is on the second line, so line 3 is the body’s fourth line. The second and third lines of an error, when present, quote that line and point at the fault, which is the quickest way to find it.
Find the expression
Section titled “Find the expression”Select Try it in the mock editor and Preview this response. The preview shows The mock server could not render this body. for an [ERROR] report, or One value could not be generated. for an error that stands in the body, with the problem in plain words. Show in the editor marks the expression it finds, which stays marked until you edit it or run Try it again.
Try it takes an [ERROR] position as counted from the top of the body. When the body’s first expression is not on its first line, the line Try it names comes before the one at fault, and it may mark nothing and say Nothing in the body matches this; it may come from a response header. Find the expression from the quoted line instead. That message also appears when the expression at fault is in one of the mock’s response headers, which are templates too.
The editor also marks the mistakes it can spot as you type, before anything is sent. Once saved, a mock with a syntax error MockHub finds without rendering is tagged Won’t render on the Mocks tab, and its page says what is wrong and where, until a save fixes it. MockHub checks each response when it changes, so on a mock with several responses the warning names the response at fault, and only that response’s tab carries the tag; the others answer as they did. A request that asks for the response at fault with X-MockHub-Response gets the same 500 as above. Write dynamic responses with templates explains the template language.
Causes
Section titled “Causes”A fake-data provider or method does not exist
Section titled “A fake-data provider or method does not exist”The random and cachedRandom helpers take a 'Provider.method' path, such as {{random 'Name.firstName'}}. The report says which half is wrong:
[ERROR] 1:2: There is no provider called 'Nope'.[ERROR] 1:2: Name has no method called 'nope'.[ERROR] 1:2: 'Name' is not a 'Provider.method' path, such as 'Name.firstName'.Names are case-sensitive: the provider starts with a capital and the method is in camel case, Name.firstName, not name.firstName or Name.first_name. Examples written for other mocking tools often use the snake-case form, which does not exist here.
- Select Suggest in the editor toolbar, or press Ctrl+Space, and pick the provider and the method from the list.
- Run Try it again.
Fake data lists every provider and method.
A fake-data method gets arguments it does not take
Section titled “A fake-data method gets arguments it does not take”[ERROR] 1:2: Address.zipCodeByState does not take state.[ERROR] 1:2: Number.numberBetween takes min and max.[ERROR] 1:2: Planet.mass: planetName takes one of JUPITER, MARS, VENUS, URANUS, MERCURY, NEPTUNE, EARTH, SATURN, PLUTO.The method exists, but not with the arguments given. The report names an argument the method does not take, the arguments it does take, or the kind of value an argument needs. It never repeats the value it was given, which can come from the request. Check the method’s arguments in Fake data, or remove them.
A helper gets no value
Section titled “A helper gets no value”[ERROR] 1:2: A helper got no value where it needs one.A helper was given a part of the request that is not there, such as {{val request.query.page}} for a request without page. Give it a default, {{val request.query.page default='1'}}, or put it inside {{#if request.query.page}}.
A random range is out of order
Section titled “A random range is out of order”[ERROR] 1:2: bound must be greater than originrandomInt needs its lower= below its upper=, and keeps both between -2147483648 and 2147483647, so a larger bound fails too. For a longer number, put fixed digits before a shorter one: 978{{randomInt lower=1000000000 upper=1999999999}} gives 13 digits.
A helper name is misspelt
Section titled “A helper name is misspelt”[ERROR] 3:13: could not find helper: 'randm'A misspelt helper name with arguments, such as {{randm 'Name.firstName'}}, fails the whole response. Correct the name, or pick it with Suggest. Template helpers lists every helper.
An expression runs into a closing brace
Section titled “An expression runs into a closing brace”[ERROR] 1:27: found: '}}}', expected: '}}'{{randomInt lower=1 upper=2}}} ^A } straight after an expression’s }}, as at the end of a JSON object, reads as }}}, which closes only an expression opened with {{{; after a {{{…}}} expression, the same goes for }}}}. The editor marks it as you type, and Try it and the request log say “A } right after this expression’s }} joins its closing braces: put a space before that }.” Write {"isbn": {{randomInt lower=1 upper=2}} }, with the space.
A block is closed with the wrong name
Section titled “A block is closed with the wrong name”[ERROR] 1:34: found: 'if', expected: 'each'{{#each (range 1 3)}}{{ this }}{{/if}} ^Every {{#each}} needs its own {{/each}}, every {{#if}} its own {{/if}}, and inner blocks close before outer ones. The caret points at the closing tag that does not fit.
A block is not closed, or closes nothing
Section titled “A block is not closed, or closes nothing”[ERROR] 1:12: found: '<EOF>', expected: '{{/'[ERROR] 1:1: found: '{{/', expected: '<EOF>'The first is a block such as {{#if}} that the body ends inside, and Try it and the request log say “A block is not closed: add its {{/…}}.” The second is a closing tag such as {{/if}} with no block open, and they say “This {{/…}} closes nothing: no block is open.” Add the missing closing tag, or remove the extra one.
cachedRandom has no key
Section titled “cachedRandom has no key”[ERROR] 1:2: The 'cachedRandom' helper requires a 'key' parameter.cachedRandom returns the same value every time it is used with the same key in one response, so the key is required: {{cachedRandom 'Name.firstName' key='firstName'}}.
A date, time zone or format is not understood
Section titled “A date, time zone or format is not understood”[ERROR] 1:2: 'fortnights' is not a unit the offset understands. Use seconds, minutes, hours, days, months or years.[ERROR] 1:2: Offset can be of the short form <amount> <unit> e.g. 8 seconds or long form now +/-<amount> <unit> e.g. now +5 years[ERROR] 1:2: 'Mars/Olympus' is not a time zone the mock server knows.[ERROR] 1:2: Unknown pattern letter: b- An offset is a signed number and a plural unit, such as
'+3 days'or'-30 minutes'. The units are seconds, minutes, hours, days, months and years; there are no weeks. - A time zone is an ID such as
Europe/WarsaworUTC. - A date format is made of pattern letters, such as
yyyy-MM-dd.
A calculation fails
Section titled “A calculation fails”An operator the math helper does not know is reported inline, where the value should be, and a division by zero fails the whole response:
[ERROR: ^ is not a valid mathematical operator][ERROR] 1:2: Division by zero.Correct the operator or the operands, and run Try it again.
A request body is not JSON or XML
Section titled “A request body is not JSON or XML”[ERROR: hello is not valid JSON]jsonPath reads the request body as JSON, and xPath as XML. When a request sends another kind of body, the error stands where the value should be, and the rest of the body renders. Make the mock match only requests whose body it can read; see Match requests.
A request value is compared with a number
Section titled “A request value is compared with a number”[ERROR] 1:3: A helper got a number where it needs text.A query parameter, header or cookie value is text, and gte, gt, lte and lt cannot order text against a number such as 2. Make it a number first: (gte (math request.query.page.first '+' 0) 2).
When the template is not rendered at all
Section titled “When the template is not rendered at all”If the body shows the expressions themselves, such as {{request.path}}, the project does not render templates. Open the project’s Settings, turn on Response templating under Responses and logging, and select Save settings. It is on for new projects, and changing it needs the Admin role.
A chaos profile’s error body is never rendered as a template, whatever the setting: an injected error returns its body exactly as written.
When the body renders but a value is wrong
Section titled “When the body renders but a value is wrong”Some mistakes produce no error at all:
- An unknown
locale=renders English. The editor warns about a locale it does not know. - A
jsonPaththat finds nothing, and a request value that does not exist, such as{{request.foo}}, produce no error. - A misspelt helper name without arguments, such as
{{randm}}, is read as a value that does not exist and renders nothing. - A
mathoperand that is not a number counts as0. - Query parameters, headers and cookies are lists, so comparing
request.query.statuswitheqis never true. Compare the first value,request.query.status.first, instead. The editor warns about these comparisons. - That first value is text, so
(eq request.query.page.first 2)is never true. Compare it with'2'.
Still stuck
Section titled “Still stuck”Report a problem with the mock’s name, the project’s address and the full error line.