<commit_before>#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010 FUJIWARA, Yusuke
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
//
#endregion -- License Terms --

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using MsgPack.Rpc.Protocols;
using MsgPack.Serialization;

namespace MsgPack.Rpc.Server.Dispatch
{
	/// <summary>
	///		<strong>This class is intended to MessagePack-RPC internal use.</strong>
	///		Defines helper methods for current service invoker implementation.
	/// </summary>
	[EditorBrowsable( EditorBrowsableState.Never )]
	public static class InvocationHelper
	{
		/// <summary>
		///		<see cref="MethodInfo"/> of <see cref="HandleArgumentDeserializationException"/>.
		/// </summary>
		internal static readonly MethodInfo HandleArgumentDeserializationExceptionMethod =
			FromExpression.ToMethod( ( Exception exception, string parameterName, bool isDebugMode ) => HandleArgumentDeserializationException( exception, parameterName, isDebugMode ) );

		/// <summary>
		///		<strong>This member is intended to MessagePack-RPC internal use.</strong>
		///		Convert specified argument deserialization error to the RPC error.
		/// </summary>
		/// <param name="exception">The exception thrown by unpacker.</param>
		/// <param name="parameterName">The parameter name failed to deserialize.</param>
		/// <returns>
		///		<see cref="RpcErrorMessage"/>.
		/// </returns>
		[EditorBrowsable( EditorBrowsableState.Never )]
		public static RpcErrorMessage HandleArgumentDeserializationException( Exception exception, string parameterName, bool isDebugMode )
		{
			return
				new RpcErrorMessage(
					RpcError.ArgumentError,
					String.Format( CultureInfo.CurrentCulture, "Argument '{0}' is invalid.", parameterName ),
					( isDebugMode && exception != null ) ? exception.ToString() : null
				);
		}

		/// <summary>
		///		<strong>This member is intended to MessagePack-RPC internal use.</strong>
		///		Convert the exception thrown by target method to the RPC error.
		/// </summary>
		/// <param name="exception">The exception thrown by target method.</param>
		/// <returns>
		///		<see cref="RpcErrorMessage"/>.
		/// </returns>
		[EditorBrowsable( EditorBrowsableState.Never )]
		public static RpcErrorMessage HandleInvocationException( long sessionId, MessageType messageType, int? messageId, string operationId, Exception exception, bool isDebugMode )
		{
			MsgPackRpcServerDispatchTrace.TraceEvent(
				MsgPackRpcServerDispatchTrace.OperationThrewException,
				"Operation threw exception. {{ \"SessionId\" : {0}, \"MessageType\" : \"{1}\", \"MessageID\" : {2}, \"OperationID\" : \"{3}\", \"Exception\" : \"{4}\" }}",
				sessionId,
				messageType,
				messageId,
				operationId,
				exception
			);

			return HandleInvocationException( exception, isDebugMode );
		}

		internal static RpcErrorMessage HandleInvocationException( Exception exception, bool isDebugMode )
		{
			ArgumentException asArgumentException;
			RpcException rpcException;
			if ( ( asArgumentException = exception as ArgumentException ) != null )
			{
				return
					new RpcErrorMessage(
						RpcError.ArgumentError,
						String.Format( CultureInfo.CurrentCulture, "Argument '{0}' is invalid.", asArgumentException.ParamName ),
						isDebugMode ? exception.ToString() : null
					);
			}
			else if ( ( rpcException = exception as RpcException ) != null )
			{
				return new RpcErrorMessage( rpcException.RpcError, rpcException.GetExceptionMessage( isDebugMode ) );
			}
			else
			{
				return
					new RpcErrorMessage( 
						RpcError.RemoteRuntimeError, 
						isDebugMode ? RpcError.RemoteRuntimeError.DefaultMessage : RpcError.RemoteRuntimeError.DefaultMessageInvariant,
						isDebugMode ? exception.ToString() : null
					);
			}
		}

		internal static void TraceInvocationResult<T>( long sessionId, MessageType messageType, int messageId, string operationId, RpcErrorMessage error, T result )
		{
			if ( error.IsSuccess )
			{
				if ( MsgPackRpcServerDispatchTrace.ShouldTrace( MsgPackRpcServerDispatchTrace.OperationSucceeded ) )<commit_msg>Change default error to CallError.<commit_after>#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010 FUJIWARA, Yusuke
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
//
#endregion -- License Terms --

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using MsgPack.Rpc.Protocols;
using MsgPack.Serialization;

namespace MsgPack.Rpc.Server.Dispatch
{
	/// <summary>
	///		<strong>This class is intended to MessagePack-RPC internal use.</strong>
	///		Defines helper methods for current service invoker implementation.
	/// </summary>
	[EditorBrowsable( EditorBrowsableState.Never )]
	public static class InvocationHelper
	{
		/// <summary>
		///		<see cref="MethodInfo"/> of <see cref="HandleArgumentDeserializationException"/>.
		/// </summary>
		internal static readonly MethodInfo HandleArgumentDeserializationExceptionMethod =
			FromExpression.ToMethod( ( Exception exception, string parameterName, bool isDebugMode ) => HandleArgumentDeserializationException( exception, parameterName, isDebugMode ) );

		/// <summary>
		///		<strong>This member is intended to MessagePack-RPC internal use.</strong>
		///		Convert specified argument deserialization error to the RPC error.
		/// </summary>
		/// <param name="exception">The exception thrown by unpacker.</param>
		/// <param name="parameterName">The parameter name failed to deserialize.</param>
		/// <returns>
		///		<see cref="RpcErrorMessage"/>.
		/// </returns>
		[EditorBrowsable( EditorBrowsableState.Never )]
		public static RpcErrorMessage HandleArgumentDeserializationException( Exception exception, string parameterName, bool isDebugMode )
		{
			return
				new RpcErrorMessage(
					RpcError.ArgumentError,
					String.Format( CultureInfo.CurrentCulture, "Argument '{0}' is invalid.", parameterName ),
					( isDebugMode && exception != null ) ? exception.ToString() : null
				);
		}

		/// <summary>
		///		<strong>This member is intended to MessagePack-RPC internal use.</strong>
		///		Convert the exception thrown by target method to the RPC error.
		/// </summary>
		/// <param name="exception">The exception thrown by target method.</param>
		/// <returns>
		///		<see cref="RpcErrorMessage"/>.
		/// </returns>
		[EditorBrowsable( EditorBrowsableState.Never )]
		public static RpcErrorMessage HandleInvocationException( long sessionId, MessageType messageType, int? messageId, string operationId, Exception exception, bool isDebugMode )
		{
			MsgPackRpcServerDispatchTrace.TraceEvent(
				MsgPackRpcServerDispatchTrace.OperationThrewException,
				"Operation threw exception. {{ \"SessionId\" : {0}, \"MessageType\" : \"{1}\", \"MessageID\" : {2}, \"OperationID\" : \"{3}\", \"Exception\" : \"{4}\" }}",
				sessionId,
				messageType,
				messageId,
				operationId,
				exception
			);

			return HandleInvocationException( exception, isDebugMode );
		}

		internal static RpcErrorMessage HandleInvocationException( Exception exception, bool isDebugMode )
		{
			ArgumentException asArgumentException;
			RpcException rpcException;
			if ( ( asArgumentException = exception as ArgumentException ) != null )
			{
				return
					new RpcErrorMessage(
						RpcError.ArgumentError,
						String.Format( CultureInfo.CurrentCulture, "Argument '{0}' is invalid.", asArgumentException.ParamName ),
						isDebugMode ? exception.ToString() : null
					);
			}
			else if ( ( rpcException = exception as RpcException ) != null )
			{
				return new RpcErrorMessage( rpcException.RpcError, rpcException.GetExceptionMessage( isDebugMode ) );
			}
			else
			{
				// TODO: More cases?
				return
					new RpcErrorMessage( 
						RpcError.CallError,
						isDebugMode ? exception.Message : RpcError.CallError.DefaultMessageInvariant,
						isDebugMode ? exception.ToString() : null
					);
			}
		}

		internal static void TraceInvocationResult<T>( long sessionId, MessageType messageType, int messageId, string operationId, RpcErrorMessage error, T result )
		{
			if ( error.IsSuccess )
			{
				if ( MsgPackRpcServerDispatchTrace.ShouldTrace( MsgPackRpcServerDispatchTrace.OperationSucceeded ) )
{'fraction_non_alphanumeric': 0.1110397946084724, 'fraction_numerical': 0.003209242618741977, 'mean_word_length': 3.6512437810945273, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4331881', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2557, 'n_words': 743}