1
\$\begingroup\$

I have a nuget package which is my first published code on nuget I am just a bit concerned on how I am returning the error to the programmer I am using a string variable to store a code.

    public List<ClsAddress> addressList = new List<ClsAddress>();
    public string mStatus { get; set; }
    public string mApiKey { get; set; }
    public string url;
    public void GetRapidAddressByPostCode(string mPostCode)
    {

        mApiKey = ConfigurationManager.AppSettings["CraftyClicksApiKey"];
        string urlToApi = ConfigurationManager.AppSettings["CraftyClicksApiUrl"];


        if (!String.IsNullOrEmpty(urlToApi))
        {
             url = String.Format(urlToApi + "?postcode={0}&response=data_formatted&key={1}",
              mPostCode, mApiKey);

        }
        else
        {
             url = String.Format("http://pcls1.craftyclicks.co.uk/json/rapidaddress?postcode={0}&response=data_formatted&key={1}",
          mPostCode, mApiKey);
        }



        //Complete XML HTTP Request
        WebRequest request = WebRequest.Create(url);
        //Complete XML HTTP Response
        WebResponse response = request.GetResponse();

        //Declare and set a stream reader to read the returned XML
        StreamReader reader = new StreamReader(response.GetResponseStream());



        string json = reader.ReadToEnd();
        // Get the requests json object and convert it to in memory dynamic
        // Note: that you are able to convert to a specific object if required.
        var jsonResponseObject = JsonConvert.DeserializeObject<dynamic>(json);
        if (jsonResponseObject != null)
        {
            if (jsonResponseObject.delivery_points != null)
            {
                //If the node list contains address nodes then move on.
                int i = 0;

                foreach (var node in jsonResponseObject.delivery_points)
                {
                    ClsAddress address = new ClsAddress()
                    {
                        AddressID = i,
                        AddressLine1 = node.line_1,
                        AddressLine2 = node.line_2,

                        County = jsonResponseObject.postal_county,
                        PostCode = jsonResponseObject.postcode,
                        Town = jsonResponseObject.town
                    };

                    addressList.Add(address);
                    i++;
                }
            }
            else
            {
                foreach (var node in jsonResponseObject)
                {
                    // Get the details of the error message and return it the user.
                    switch ((string)node.Value)
                    {
                        case "0001":
                            mStatus = "Post Code not found";
                            break;
                        case "0002":
                            mStatus = "Invalid Post Code format";
                            break;
                        case "7001":
                            mStatus = "Demo limit exceeded";
                            break;
                        case "8001":
                            mStatus = "Invalid or no access token";
                            break;
                        case "8003":
                            mStatus = "Account credit allowance exceeded";
                            break;
                        case "8004":
                            mStatus = "Access denied due to access rules";
                            break;
                        case "8005":
                            mStatus = "Access denied, account suspended";
                            break;
                        case "9001":
                            mStatus = "Internal server error";
                            break;
                        default:
                            mStatus = (string)node.Value;
                            break;
                    }
                }
            }





    }
    }

My question is surround the codes

 case "0001":
    mStatus = "Post Code not found";
 break;

Is this the best way of doing this, or would I be better using a custom exception class. And if so what would be my best way of doing this.

The above is based on the interpretation of the documentation for the third party api.

https://craftyclicks.co.uk/docs/postcode-lookup/#testing

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Is this your real code? This loop foreach (var node in jsonResponseObject) does not make much sense. If there is more then one node then the other ones will overwirte mStatus and you'll get only the last one. \$\endgroup\$ – t3chb0t Oct 6 '17 at 10:38
  • \$\begingroup\$ There will only be this one node if there are any suggestions please make them in a answer would be good \$\endgroup\$ – David Buckley Oct 6 '17 at 11:01
  • \$\begingroup\$ @t3chb0t it is based on this craftyclicks.co.uk/docs/postcode-lookup/#testing \$\endgroup\$ – David Buckley Oct 6 '17 at 11:02
1
\$\begingroup\$

You could create an enum with your error codes and map the node.Value to enum to get the error description.

\$\endgroup\$
1
  • \$\begingroup\$ You have not provided an example merly a description how would one do this ?. \$\endgroup\$ – David Buckley Oct 7 '17 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.