fromHttpResponse static method
Implementation
static OpenAIRequestException fromHttpResponse(http.Response r) {
try {
final obj = jsonDecode(r.body) as Map<String, dynamic>;
if (obj.containsKey('error') && obj['error'] is Map) {
final err = obj['error'] as Map;
return _subclassForCode(
status: r.statusCode,
code: err['code'] as String?,
message: err['message'] as String? ?? 'Unknown OpenAI error',
param: err['param'] as String?,
);
}
} catch (_) {/* fall through if body wasn’t JSON */}
// Non-JSON: keep a short preview so logs stay readable.
final preview = r.body.length > 300 ? '${r.body.substring(0, 300)}…' : r.body;
return _subclassForCode(
status: r.statusCode,
code: null,
message: 'HTTP ${r.statusCode}',
param: null,
bodyPreview: preview,
);
}