Example #1
0
        /// <summary>
        /// Converts SQL error int to plain english error message
        /// </summary>
        /// <param name="errorCode">The error code from the sp</param>
        public static void ParseCreateCommentSpError(int errorCode)
        {
            var exception = new ApiException("Unknown internal error has occurred");
            switch (errorCode)
            {
                case 1:
                    exception = ApiException.GetError(ErrorType.ForumUnknown);
                    break;

                case 2:
                    exception = ApiException.GetError(ErrorType.ForumClosed);
                    break;

                case 3:
                    exception = ApiException.GetError(ErrorType.ForumReadOnly);
                    break;
            }

            throw exception;
        }
        /// <summary>
        /// Throws a new WebProtocalException. Debug builds throw the exception along with the description and inner exception, release builds
        /// only throw the StatusCode.
        /// </summary>
        /// <param name="e">the api exception</param>
        public DnaWebProtocolException(ApiException e)
        {
            ErrorType = e.type;
            switch (e.type)
            {
                case ErrorType.FailedTermsAndConditions: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.MissingUserCredentials: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.MissingEditorCredentials: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.UserIsBanned: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.SiteIsClosed: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.EmptyText: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ExceededTextLimit: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.MinCharLimitNotReached: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.XmlFailedParse: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ProfanityFoundInText: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ForumUnknown: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.ForumClosed: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ForumReadOnly: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidForumUid: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidForumParentUri: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidForumTitle: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.UnknownSite: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.MultipleRatingByUser: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.RatingExceedsMaximumAllowed: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidProcessPreModState: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidModerationStatus: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidForumClosedDate: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.UnknownFormat: StatusCode = HttpStatusCode.NotImplemented; break;
                case ErrorType.InvalidUserId: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidRatingValue: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.InvalidPostStyle: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.MissingUserList: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.NotSecure: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.CommentNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.CategoryNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.MonthSummaryNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.IndexNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.UserNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.ForumOrThreadNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.GuideMLTransformationFailed: StatusCode = HttpStatusCode.InternalServerError; break;
                case ErrorType.ThreadPostNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.ThreadNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.InvalidThreadID: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ForumIDNotWellFormed: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.MaintenanceMode: StatusCode = HttpStatusCode.ServiceUnavailable; break;  
                case ErrorType.AlreadyLinked: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ArticleNotFound: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.NoResults: StatusCode = HttpStatusCode.NotFound; break;
                case ErrorType.NotForReview: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.AddIntoReviewForumFailed: StatusCode = HttpStatusCode.InternalServerError; break;
                case ErrorType.InvalidH2G2Id: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.MissingGuideML: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.MissingSubject: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.UserDoesNotHavePermissionToEditArticle: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.ArticleContainsURLs: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.ArticleContainsEmailAddress: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.WrongStatus: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.AlreadyRecommended: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.OwnEntry: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.NotInReview: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.UserBlockedSubscriptions: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.CantSubscribe: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.UnableToHideUnHideThread: StatusCode = HttpStatusCode.BadRequest; break;
                case ErrorType.NotAuthorized: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.MissingUserAttributes: StatusCode = HttpStatusCode.Unauthorized; break;
                case ErrorType.InvalidContactEmail: StatusCode = HttpStatusCode.BadRequest; break;
                default: StatusCode = HttpStatusCode.InternalServerError; break;
            }

            LogEntry entry = new LogEntry()
            {
                Message = String.Format("{0}-{1}-{2}", StatusCode, e.type, e.Message),
                Severity = System.Diagnostics.TraceEventType.Error,
            };
            Logger.Write(entry);
            
            /*
            WebFormat.format format = WebFormat.format.UNKNOWN;
            string contentType = "";
            WebFormat.getReturnFormat((WebOperationContext.Current.IncomingRequest.ContentType == null ? "" : WebOperationContext.Current.IncomingRequest.ContentType),
            ref contentType, ref format);

            if (format == WebFormat.format.HTML)
            {
                string ptrt = WebFormat.GetPtrtWithResponse(ErrorType.ToString());
                //do response redirect...
                WebOperationContext.Current.OutgoingResponse.Location = ptrt;
                StatusCode = System.Net.HttpStatusCode.MovedPermanently;

            }*/
            this.detailWriter = new StringDetailWriter() { ErrorCode = e.type.ToString(), Detail = e.Message, StatusCode = StatusCode, innerException = e.InnerException };
        }
Example #3
0
        public static ApiException GetError(ErrorType type, Exception innerException)
        {
            ApiException error;
            switch (type)
            {
                case ErrorType.MissingUserCredentials:
                    error = new ApiException("Missing user credentials.", innerException);
                    break;
                case ErrorType.FailedTermsAndConditions:
                    error = new ApiException("Failed terms and conditions.", innerException);
                    break;
                case ErrorType.MissingEditorCredentials:
                    error = new ApiException("Missing editor credentials.", innerException);
                    break;
                case ErrorType.UserIsBanned:
                    error = new ApiException("User is banned.", innerException);
                    break;
                case ErrorType.SiteIsClosed:
                    error = new ApiException("Site is closed.", innerException);
                    break;
                case ErrorType.EmptyText:
                    error = new ApiException("Text is null or blank.", innerException);
                    break;
                case ErrorType.ExceededTextLimit:
                    error = new ApiException("Text exceeds maximum character limit.", innerException);
                    break;
                case ErrorType.XmlFailedParse:
                    error = new ApiException("The xml provided failed to parse correctly.", innerException);
                    break;
                case ErrorType.ProfanityFoundInText:
                    error = new ApiException("Profanity filter triggered error.", innerException);
                    break;
                case ErrorType.ForumUnknown:
                    error = new ApiException("Forum is unknown and cannot be added to.", innerException);
                    break;
                case ErrorType.ForumClosed:
                    error = new ApiException("Forum is closed and cannot be added to.", innerException);
                    break;
                case ErrorType.ForumReadOnly:
                    error = new ApiException("Forum is readonly and cannot be added to.", innerException);
                    break;
                case ErrorType.InvalidForumUid:
                    error = new ApiException("Forum uid is empty, null or exceeds 255 characters.", innerException);
                    break;
                case ErrorType.InvalidForumParentUri:
                    error = new ApiException("Forum parent uri is empty, null or not from a bbc.co.uk domain.",
                                              innerException);
                    break;
                case ErrorType.InvalidForumTitle:
                    error = new ApiException("Forum title uri is empty or null.", innerException);
                    break;
                case ErrorType.UnknownSite:
                    error = new ApiException("Site reference is unknown.", innerException);
                    break;
                case ErrorType.MultipleRatingByUser:
                    error = new ApiException("User has already rated this content.", innerException);
                    break;
                case ErrorType.RatingExceedsMaximumAllowed:
                    error = new ApiException("Rating exceeds allowed maximum.", innerException);
                    break;
                case ErrorType.InvalidProcessPreModState:
                    error =
                        new ApiException(
                            "Ratings not possible in pre-moderation with 'ProcessPreMod' site option set.",
                            innerException);
                    break;
                case ErrorType.InvalidModerationStatus:
                    error = new ApiException("The ModerationServiceGroup is not valid.", innerException);
                    break;
                case ErrorType.InvalidForumClosedDate:
                    error = new ApiException("The closed date is not valid.", innerException);
                    break;
                case ErrorType.UnknownFormat:
                    error = new ApiException("The requested format is not implemented or unknown.", innerException);
                    break;
                case ErrorType.InvalidUserId:
                    error = new ApiException("User must be a integer", innerException);
                    break;
                case ErrorType.InvalidRatingValue:
                    error = new ApiException("The rating object must be between 0 and 255", innerException);
                    break;
                case ErrorType.InvalidThreadID:
                    error = new ApiException("The threadID must be of valid type.", innerException);
                    break;
                case ErrorType.InvalidPostStyle:
                    error = new ApiException("The postStyle must be of valid type.", innerException);
                    break;
                case ErrorType.InvalidH2G2Id:
                    error = new ApiException("H2G2 Id is not well formed", innerException);
                    break;                    
                case ErrorType.CommentNotFound:
                    error = new ApiException("The comment with the given id could not be found.", innerException);
                    break;
                case ErrorType.MinCharLimitNotReached:
                    error = new ApiException("Text is below the minimum character limit.", innerException);
                    break;

                case ErrorType.MissingUserList:
                    error = new ApiException("No user ids passed in.", innerException);
                    break;
                case ErrorType.NotSecure:
                    error = new ApiException("Not a secure posting.", innerException);
                    break;
                case ErrorType.CategoryNotFound:
                    error = new ApiException("Category not found.", innerException);
                    break;
                case ErrorType.MonthSummaryNotFound:
                    error = new ApiException("Month summary not found.", innerException);
                    break;
                case ErrorType.IndexNotFound:
                    error = new ApiException("Index not found.", innerException);
                    break;
                case ErrorType.UserNotFound:
                    error = new ApiException("User not found.", innerException);
                    break;
                case ErrorType.ForumOrThreadNotFound:
                    error = new ApiException("Forum or Thread not found.", innerException);
                    break;
                case ErrorType.GuideMLTransformationFailed:
                    error = new ApiException("GuideML Transformation Failed.", innerException);
                    break;
                case ErrorType.ThreadPostNotFound:
                    error = new ApiException("Thread post not found.", innerException);
                    break;
                case ErrorType.ThreadNotFound:
                    error = new ApiException("Thread not found.", innerException);
                    break;
                case ErrorType.ForumIDNotWellFormed:
                    error = new ApiException("ForumID not well formed.", innerException);
                    break;

                case ErrorType.MaintenanceMode:
                    error = new ApiException("Service under maintenance mode.", innerException);
                    break;
                    
                case ErrorType.AlreadyLinked:
                    error = new ApiException("Already linked.", innerException);
                    break;
                case ErrorType.ArticleNotFound:
                    error = new ApiException("Article not found.", innerException);
                    break;
                case ErrorType.MissingSubject:
                    error = new ApiException("Missing Subject.", innerException);
                    break;
                case ErrorType.MissingGuideML:
                    error = new ApiException("Missing GuideML.", innerException);
                    break;
                case ErrorType.NoResults:
                    error = new ApiException("No results.", innerException);
                    break;
                case ErrorType.NotForReview:
                    error = new ApiException("Not for Review.", innerException);
                    break;                   
                case ErrorType.AddIntoReviewForumFailed:
                    error = new ApiException("Failed to add into review forum.", innerException);
                    break;
                case ErrorType.UserDoesNotHavePermissionToEditArticle:
                    error = new ApiException("User does not have permission to edit article", innerException);
                    break;

                case ErrorType.ArticleContainsURLs:
                    error = new ApiException("Article contains URLs", innerException);
                    break;

                case ErrorType.ArticleContainsEmailAddress:
                    error = new ApiException("Article contains email addresses", innerException);
                    break;
                case ErrorType.WrongStatus:
                    error = new ApiException("Entry is not a public user entry", innerException);
                    break;
                case ErrorType.AlreadyRecommended:
                    error = new ApiException("Entry has already been recommended", innerException);
                    break;
                case ErrorType.OwnEntry:
                    error = new ApiException("Recommender is author", innerException);
                    break;
                case ErrorType.NotInReview:
                    error = new ApiException("Not in appropriate review forum", innerException);
                    break;
                case ErrorType.UserBlockedSubscriptions:
                    error = new ApiException("This user has blocked all subscriptions.", innerException);
                    break;
                case ErrorType.CantSubscribe:
                    error = new ApiException("You cannot subscribe to this user.", innerException);
                    break;
                case ErrorType.UnableToHideUnHideThread:
                    error = new ApiException("Unable to hide/unhide thread.", innerException);
                    break;
                case ErrorType.NotAuthorized:
                    error = new ApiException("User does not have permission.", innerException);
                    break;

                case ErrorType.MissingUserAttributes:
                    error = new ApiException("Unable to access client IP or BBC Uid value.", innerException);
                    break;

                case ErrorType.PostFrequencyTimePeriodNotExpired:
                    error = new ApiException("The time period between posting has not expired yet.", innerException);
                    break;
                    
                case ErrorType.InvalidEntryId:
                    error = new ApiException("Entry Id is invalid or out of range", innerException);
                    break;

                case ErrorType.InvalidContactEmail:
                    error = new ApiException("Invalid Contact Email Provided", innerException);
                    break;

                case ErrorType.MissingContactEmail:
                    error = new ApiException("NO Contact Email Provided For Site", innerException);
                    break;

                default:
                    error = new ApiException("Unknown error has occurred.", innerException);
                    break;


            }
            error.type = type;
            return error;
        }
Example #4
0
        private void TweetToCommentExceptionHandler(ApiException exception, int auditId)
        {
            var reason = string.Empty;

            switch (exception.type)
            {
                case ErrorType.EmptyText:
                    reason = "Empty Tweet";
                    break;
                case ErrorType.ForumClosed:
                    reason = "Forum Closed";
                    break;
                case ErrorType.ForumUnknown:
                    reason = "Forum Unknown";
                    break;
                case ErrorType.ProfanityFoundInText:
                    reason = "Profanity Found";
                    break;
                case ErrorType.UnknownSite :
                    reason = "Unknown Site";
                    break;
                case ErrorType.UserIsBanned:
                    reason = "User Is Banned";
                    break;
                case ErrorType.SiteIsClosed:
                    reason = "Site Closed";
                    break;
                default:
                    break;
            }

            UpdateTwitterAudit(auditId, reason);
        }
Example #5
0
        /// <summary>
        /// Creates new post after checking relevant items...
        /// </summary>
        /// <param name="cacheManager"></param>
        /// <param name="readerCreator"></param>
        /// <param name="site"></param>
        /// <param name="viewingUser"></param>
        /// <param name="siteList"></param>
        /// <param name="forumId"></param>
        /// <param name="ThreadId"></param>
        /// <param name="_iPAddress"></param>
        /// <param name="bbcUidCookie"></param>
        public void PostToForum(ICacheManager cacheManager, IDnaDataReaderCreator readerCreator, ISite site,
            IUser viewingUser, ISiteList siteList, string _iPAddress, Guid bbcUidCookie, int forumId)
        {
            if (viewingUser.UserId == 0)
            {
                throw ApiException.GetError(ErrorType.NotAuthorized);
            }

            ForumSource forumSource = ForumSource.CreateForumSource(cacheManager, readerCreator, null, forumId, ThreadId, site.SiteID, false, false, false);
            if (forumSource == null)
            {
                throw ApiException.GetError(ErrorType.ForumUnknown);
            }

            bool isNotable = viewingUser.IsNotable;

            ForumHelper helper = new ForumHelper(readerCreator);
            bool ignoreModeration = viewingUser.IsEditor || viewingUser.IsSuperUser;
            // Check 4) check ThreadId exists and user has permission to write
            if (!ignoreModeration)
            {
                if (ThreadId != 0)
                {
                    bool canReadThread = false;
                    bool canWriteThread = false;
                    helper.GetThreadPermissions(viewingUser.UserId, ThreadId, ref canReadThread, ref canWriteThread);
                    if (!canReadThread)
                    {
                        throw ApiException.GetError(ErrorType.NotAuthorized);
                    }
                    if (!canWriteThread)
                    {
                        throw ApiException.GetError(ErrorType.ForumReadOnly);
                    }
                }
                else
                {
                    bool canReadForum = false;
                    bool canWriteForum = false;
                    helper.GetForumPermissions(viewingUser.UserId, forumId, ref canReadForum, ref canWriteForum);
                    if (!canReadForum)
                    {
                        throw ApiException.GetError(ErrorType.NotAuthorized);
                    }
                    if (!canWriteForum)
                    {
                        throw ApiException.GetError(ErrorType.ForumReadOnly);
                    }
                }
            }
        
            if (viewingUser.IsBanned)
            {
                throw ApiException.GetError(ErrorType.UserIsBanned);
            }
            
            if (!ignoreModeration && (site.IsEmergencyClosed || site.IsSiteScheduledClosed(DateTime.Now)))
            {
                throw ApiException.GetError(ErrorType.SiteIsClosed);
            }
            if (String.IsNullOrEmpty(Text))
            {
                throw ApiException.GetError(ErrorType.EmptyText);
            }
            try
            {

                int maxCharCount = siteList.GetSiteOptionValueInt(site.SiteID, "CommentForum", "MaxCommentCharacterLength");
                string tmpText = StringUtils.StripFormattingFromText(Text);
                if (maxCharCount != 0 && tmpText.Length > maxCharCount)
                {
                    throw ApiException.GetError(ErrorType.ExceededTextLimit);
                }
            }
            catch (SiteOptionNotFoundException)
            {
            }
            try
            {
                //check for option - if not set then it throws exception
                int minCharCount = siteList.GetSiteOptionValueInt(site.SiteID, "CommentForum", "MinCommentCharacterLength");
                string tmpText = StringUtils.StripFormattingFromText(Text);
                if (minCharCount != 0 && tmpText.Length < minCharCount)
                {
                    throw ApiException.GetError(ErrorType.MinCharLimitNotReached);
                }
            }
            catch (SiteOptionNotFoundException)
            {
            }

            //Only check xml parsing for richtext plain text we want what is there so smileys etc work
            //if (this.Style == PostStyle.Style.richtext)
            //{
            //    string errormessage = string.Empty;
            //    // Check to make sure that the comment is made of valid XML
            //    if (!HtmlUtils.ParseToValidGuideML(Text, ref errormessage))
            //    {
            //        throw ApiException.GetError(ErrorType.XmlFailedParse);
            //    }
            //}

            bool forceModeration;
            string matchingProfanity= string.Empty;
            string profanityxml = string.Empty;
            string postString = Subject + " " + Text;
            List<Term> terms = null;
            if (InReplyTo > 0)
            {//only check text if not first post
                postString = Text;
            }
            CheckForProfanities(site, postString, out forceModeration, out matchingProfanity, out terms, forumId);

            if (false == string.IsNullOrEmpty(matchingProfanity))
            {
                matchingProfanity = "Filtered terms: " + matchingProfanity; // Adding an extra bit of information for clarity
            }

            if (terms != null && terms.Count > 0)
            {
                profanityxml = new Term().GetProfanityXML(terms);
            }

            //check posting frequency
            if (!viewingUser.IsEditor && !viewingUser.IsSuperUser && !viewingUser.IsNotable)
            {
                SecondsToWait = CheckPostFrequency(readerCreator, viewingUser.UserId, site.SiteID);
                if (SecondsToWait != 0)
                {
                    var error =  ApiException.GetError(ErrorType.PostFrequencyTimePeriodNotExpired);
                    ApiException newError = new ApiException(
                        error.Message + " You must wait " + SecondsToWait.ToString() + " more seconds before posting.",
                        error.type);
                    throw newError;
                }
            }


            bool forcePreModeration = false;
            // PreModerate first post in discussion if site premoderatenewdiscussions option set.
            if ((InReplyTo == 0) && siteList.GetSiteOptionValueBool(site.SiteID, "Moderation", "PreModerateNewDiscussions"))
            {
                if (!ignoreModeration && !isNotable)
                {
                    forcePreModeration = true;
                }
            }

            

            if (forumSource.Type == ForumSourceType.Journal && ThreadId == 0)
            {
                CreateJournalPost(readerCreator, site.SiteID, viewingUser.UserId, viewingUser.UserName, forumId, false, _iPAddress, bbcUidCookie, forceModeration);
            }
            else
            {
                CreateForumPost(readerCreator, viewingUser.UserId, forumId, ignoreModeration, isNotable, _iPAddress, bbcUidCookie, false, false, forcePreModeration, forceModeration, matchingProfanity, profanityxml);
            }
        }