public void ShouldSerializeSimpleError() { var error = new MatrixError("foobar", "foobar was invalid"); var json = JsonConvert.SerializeObject(error); Assert.AreEqual("{\"errcode\":\"foobar\",\"error\":\"foobar was invalid\"}", json); }
/// <summary> /// Attempts to extract a <see cref="MatrixError" /> from an <see cref="ApiException" />. /// </summary> /// <param name="exception">The exception instance to extract an error from.</param> /// <param name="error"> /// When this method returns, contains the extracted error, if one was found; otherwise, /// the default value for <see cref="MatrixError" />. /// </param> /// <returns><c>true</c> if an error was found; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="exception" /> is <c>null</c>.</exception> public static bool TryGetError([NotNull] this ApiException exception, out MatrixError error) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } try { error = exception.GetError(); return(true); } catch (JsonSerializationException) { error = default; return(false); } }
/// <summary> /// Attempts to deserialize a <see cref="MatrixError" /> from the response. /// </summary> /// <param name="response">The response to read from.</param> /// <param name="error"> /// When this method returns, contains the deserialized <see cref="MatrixError" />, if successful; /// otherwise, the default value for <see cref="MatrixError" />. /// </param> /// <typeparam name="T">The type contained in the response.</typeparam> /// <returns> /// <c>true</c> if an error was successfully deserialized; otherwise, <c>false</c>. /// </returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="response" /> is <c>null</c>.</exception> public static bool TryGetError <T>([NotNull] this Response <T> response, out MatrixError error) { if (response == null) { throw new ArgumentNullException(nameof(response)); } try { error = response.GetError(); return(true); } catch (JsonSerializationException) { error = default; return(false); } }