Ensure that errors encoding API results produce actionable log entries.
Part of the follow-up to T210550.
Change-Id: I6f311451e3b07b540f14352ce25af9d74a053d19
}
$data = $this->getResult()->getResultData( null, $transform );
$json = FormatJson::encode( $data, $this->getIsHtml(), $opt );
+ if ( $json === false ) {
+ // This should never happen, but it's a bug which could crop up
+ // if you use ApiResult::NO_VALIDATE for instance.
+ // @codeCoverageIgnoreStart
+ $this->dieDebug( __METHOD__, 'Unable to encode API result as JSON' );
+ // @codeCoverageIgnoreEnd
+ }
// T68776: OutputHandler::mangleFlashPolicy() avoids a nasty bug in
// Flash, but what it does isn't friendly for the API, so we need to
// Cross-domain mangling
[ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],
+
+ // Invalid UTF-8: bytes 192, 193, and 245-255 are off-limits
+ [
+ [ 'foo' => "\xFF" ],
+ "{\"foo\":\"\u{FFFD}\"}", // Mangled when validated (T210548)
+ ],
+ [
+ [ 'foo' => "\xFF" ],
+ new MWException(
+ 'Internal error in ApiFormatJson::execute: ' .
+ 'Unable to encode API result as JSON'
+ ),
+ [],
+ [ 'flags' => ApiResult::NO_VALIDATE ],
+ ],
+ // NaN is also not allowed
+ [
+ [ 'foo' => NAN ],
+ new InvalidArgumentException(
+ 'Cannot add non-finite floats to ApiResult'
+ ),
+ ],
+ [
+ [ 'foo' => NAN ],
+ new MWException(
+ 'Internal error in ApiFormatJson::execute: ' .
+ 'Unable to encode API result as JSON'
+ ),
+ [],
+ [ 'flags' => ApiResult::NO_VALIDATE ],
+ ],
] )
// @todo Test rawfm
);
$options = [ 'class' => $options ];
}
$printerName = $options['name'] ?? $this->printerName;
+ $flags = $options['flags'] ?? 0;
$context = new RequestContext;
$context->setRequest( new FauxRequest( $params, true ) );
$result = $main->getResult();
$result->addArrayType( null, 'default' );
foreach ( $data as $k => $v ) {
- $result->addValue( null, $k, $v );
+ $result->addValue( null, $k, $v, $flags );
}
$ret = [];