<commit_before>import Leaf
import Authentication
import Sugar

public final class RequireRoleTag<U: AdminPanelUserType>: TagRenderer {
    public func render(tag: TagContext) throws -> Future<TemplateData> {
        try tag.requireParameterCount(1)
        
        let container = try tag.container.make(
            CurrentUserContainer<U>.self
        )
        
        let body = try tag.requireBody()
        
        return tag.serializer.serialize(ast: body).map(to: TemplateData.self) { body in
            guard
                let roleString: String = tag.parameters[0].string,
                let requiredRole = U.Role.init(roleString)
            else {
                throw Abort(.internalServerError, reason: "Invalid role requirement")
            }
            
            if requiredRole.weight == 0 {
                throw tag.error(reason: "User role is at minimum. no requirement needed")
            }
            
            guard let userRole = container.user?.role else {
                throw Abort(.internalServerError, reason: "Invalid user role")
            }
            
            if userRole >= requiredRole {
                let parsedBody = String(data: body.data, encoding: .utf8) ?? ""
                return .string(parsedBody)
            } else {
                return TemplateData.string("")
            }
        }
    }
}<commit_msg>Throwing tag errors instead of Abort in requireRole tag<commit_after>import Leaf
import Authentication
import Sugar

public final class RequireRoleTag<U: AdminPanelUserType>: TagRenderer {
    public func render(tag: TagContext) throws -> Future<TemplateData> {
        try tag.requireParameterCount(1)
        
        let container = try tag.container.make(
            CurrentUserContainer<U>.self
        )
        
        let body = try tag.requireBody()
        
        return tag.serializer.serialize(ast: body).map(to: TemplateData.self) { body in
            guard
                let roleString: String = tag.parameters[0].string,
                let requiredRole = U.Role.init(roleString)
            else {
                throw tag.error(reason: "Invalid role requirement")
            }
            
            if requiredRole.weight == 0 {
                throw tag.error(reason: "Required role is at minimum. No requirement needed")
            }
            
            guard let userRole = container.user?.role else {
                throw tag.error(reason: "Invalid user role")
            }
            
            if userRole >= requiredRole {
                let parsedBody = String(data: body.data, encoding: .utf8) ?? ""
                return .string(parsedBody)
            } else {
                return TemplateData.string("")
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.083633741888969, 'fraction_numerical': 0.002883922134102379, 'mean_word_length': 1.6353276353276354, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1877030', 'n_tokens_mistral': 688, 'n_tokens_neox': 664, 'n_words': 182}