모듈:ErrorWatcher
보이기
	
	
틀의 인자에 오류가 포함되어 있을 경우, 그 오류를 그대로 전달합니다. 틀이 #expr같이 입력값에 민감한 함수를 사용할 때, 인자로 오류가 들어올 경우 원본 오류 메시지를 유지해서 디버깅 난이도를 줄일 수 있습니다.
define[원본 편집]
{{#invoke:ErrorWatcher|define|(틀 이름)|(내용)|...(감시할 인자 이름)}}
틀을 정의할 때 사용할 수 있는 함수입니다. 이 함수를 사용한 틀은 평범하게 끼워넣어도 오류 전달 기능이 작동합니다. 이 함수를 사용한 틀이 호출될 때 오류가 있는 인자가 주어지면 내용이 표시되지 않고, 오류가 없을 때만 내용이 표시됩니다. 감시할 인자 이름을 선택할 수 있습니다. 감시할 인자를 명시한 경우, 해당 인자만 오류가 있는지 확인하며, 명시되지 않은 경우 모든 인자를 확인합니다.
code_blocks 코드
    {{모듈:ErrorWatcher/연습장|World}}
    code
description 결과
    Hello, World!
code_blocks 코드
    {{모듈:ErrorWatcher/연습장|1=<strong class="error">오류 메시지</strong>}}
    code
description 결과
    모듈:ErrorWatcher/연습장 received error from argument '1': 오류 메시지
call[원본 편집]
{{#invoke:ErrorWatcher|call|(틀 및 파서 함수 이름)|...(인자)}}
틀을 호출할 때 사용할 수 있는 함수입니다. define 함수를 사용하지 않은 틀에도 오류 전달 기능을 사용할 수 있습니다. 기본적으로 모든 인자의 오류를 감시합니다.
code_blocks 코드
    {{#invoke:ErrorWatcher|call|CGI|{{#expr:{{CGI}}+20}}}}
    code
description 결과
    
code_blocks 코드
    {{#invoke:ErrorWatcher|call|CGI|{{#expr:{CGI}+20}}}}
    code
description 결과
    CGI received error from argument '1': 수식 오류: 알 수 없는 "{" 구두점 문자입니다.
| 위 설명은 모듈:ErrorWatcher/설명문서의 내용을 가져와 보여주고 있습니다. (편집 | 역사) 이 모듈에 대한 수정 연습과 시험은 연습장 (편집 | 차이)과 시험장 (만들기)에서 할 수 있습니다. 분류는 /설명문서에 넣어주세요. 이 모듈에 딸린 문서.  | 
local p = {}
local frame = mw.getCurrentFrame()
function iferror(content)
	return frame:callParserFunction('#iferror', content, 1, 0) == '1'
end
function titleExists(title, namespace)
	local titleObj = mw.title.new(title, namespace)
	
	if titleObj then
		return titleObj.exists
	else
		return false
	end
end
function pairstotable(pair)
	local tbl = {}
	
	for key, value in pair do
		tbl[key] = value
	end
	
	return tbl
end
function p.define(frame)
	local parent = frame:getParent()
	local args = pairstotable(pairs(frame.args))
	local name = table.remove(args, 1)
	local content = table.remove(args, 1)
	local catchedArgs = {}
	
	for key in (#args > 0 and ipairs(args) or pairs(parent.args)) do
		if iferror(parent.args[key]) then
			table.insert(catchedArgs, key)
		end
	end
	
	if #catchedArgs > 0 then
		local html = mw.html.create('strong'):attr('class', 'error')
		local link = titleExists(name) and "[[" .. name .. "]]" or name
		html:wikitext(link .. " received error".. (#catchedArgs > 1 and "s" or ""))
		for key in ipairs(catchedArgs) do
			html:wikitext(" from argument '" .. key .. "': " .. parent.args[key])
		end
		
		return html:done()
	end
	
	return content
end
function p.call(frame)
	local args = pairstotable(pairs(frame.args))
	local name = table.remove(args, 1)
	local catchedArgs = {}
	local result = ""
	local title = mw.title.new(name, "Template")
	
	for key, value in pairs(args) do
		if iferror(value) then
			table.insert(catchedArgs, key)
		end
	end
	
	if #catchedArgs > 0 then
		local html = mw.html.create('strong'):attr('class', 'error')
		local link = title.exists and "[[" .. title.fullText .. '|' .. name .. "]]" or name
		html:wikitext(link .. " received error".. (#catchedArgs > 1 and "s" or ""))
		for key in ipairs(catchedArgs) do
			html:wikitext(" from argument '" .. key .. "': " .. args[key])
		end
		
		return html:done()
	end
	
	if titleExists(name, "Template") then
		result = frame:expandTemplate{title = name, args = args}
	else
		result = frame:callParserFunction(name, args)
	end
	
	return result
end
function p.caller(frame)
	return p.call(frame:getParent())
end
return p