Skip to content

Commit

Permalink
Show warnings instead of panic if some profile is missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisCatus committed Dec 15, 2014
1 parent bae39a2 commit 6239826
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 20 deletions.
9 changes: 9 additions & 0 deletions omega-i18n/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@
"options_formInvalid": {
"message": "Please correct the errors in this page."
},
"options_profileNotFound": {
"message": "Profile $PROFILE$ does not exist! The options may be corrupted.",
"placeholders": {
"profile": {
"content": "$1",
"example": "Example"
}
}
},
"options_resetSuccess": {
"message": "Options reset."
},
Expand Down
9 changes: 9 additions & 0 deletions omega-i18n/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@
"options_formInvalid": {
"message": "请更正这个页面中的错误。"
},
"options_profileNotFound": {
"message": "情景模式 $PROFILE$ 不存在!选项可能已经损坏。",
"placeholders": {
"profile": {
"content": "$1",
"example": "Example"
}
}
},
"options_resetSuccess": {
"message": "选项已经重置。"
},
Expand Down
9 changes: 9 additions & 0 deletions omega-i18n/zh_HK/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@
"options_formInvalid": {
"message": "請更正這個頁面中的錯誤。"
},
"options_profileNotFound": {
"message": "情景模式 $PROFILE$ 不存在!選項可能已經損壞。",
"placeholders": {
"profile": {
"content": "$1",
"example": "Example"
}
}
},
"options_resetSuccess": {
"message": "選項已經重置。"
},
Expand Down
9 changes: 9 additions & 0 deletions omega-i18n/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,15 @@
"options_formInvalid": {
"message": "請更正這個頁面中的錯誤。"
},
"options_profileNotFound": {
"message": "情景模式 $PROFILE$ 不存在!選項可能已經損壞。",
"placeholders": {
"profile": {
"content": "$1",
"example": "Example"
}
}
},
"options_resetSuccess": {
"message": "選項已經重置。"
},
Expand Down
17 changes: 11 additions & 6 deletions omega-pac/src/pac_generator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ module.exports =
compressed_ast.mangle_names()
compressed_ast

script: (options, profile) ->
script: (options, profile, args) ->
if typeof profile == 'string'
profile = Profiles.byName(profile, options)
refs = Profiles.allReferenceSet(profile, options)
refs = Profiles.allReferenceSet(profile, options,
profileNotFound: args?.profileNotFound)

profiles = new U2.AST_Object properties:
for key, name of refs when key != '+direct'
new U2.AST_ObjectKeyVal(
key: key
value: Profiles.compile(Profiles.byName(name, options) ? profile),
)
p = if typeof profile == 'object' and profile.name == name
profile
else
Profiles.byName(name, options)
if not p?
p = Profiles.profileNotFound(name, args?.profileNotFound)
new U2.AST_ObjectKeyVal(key: key, value: Profiles.compile(p))

factory = new U2.AST_Function(
argnames: [
Expand Down
44 changes: 35 additions & 9 deletions omega-pac/src/profiles.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,48 @@ module.exports = exports =
return cache.directReferenceSet if cache.directReferenceSet
handler = exports._handler(profile)
cache.directReferenceSet = handler.directReferenceSet.call(exports, profile)
allReferenceSet: (profile, options, opt_out) ->

profileNotFound: (name, action) ->
if not action?
throw new Error("Profile #{name} does not exist!")
if typeof action == 'function'
action = action(name)
if typeof action == 'object' and action.profileType
return action
switch action
when 'ignore'
return null
when 'dumb'
return exports.create({
name: name
profileType: 'VirtualProfile'
defaultProfileName: 'direct'
})
throw action

allReferenceSet: (profile, options, opt_args) ->
o_profile = profile
profile = exports.byName(profile, options)
throw new Error("Profile #{o_profile} does not exist!") if not profile?
result = opt_out ? {}
result[exports.nameAsKey(profile.name)] = profile.name
for key, name of exports.directReferenceSet(profile)
exports.allReferenceSet(name, options, result)
profile ?= exports.profileNotFound?(o_profile, opt_args.profileNotFound)
opt_args ?= {}
has_out = opt_args.out?
result = opt_args.out ?= {}
if profile
result[exports.nameAsKey(profile.name)] = profile.name
for key, name of exports.directReferenceSet(profile)
exports.allReferenceSet(name, options, opt_args)
delete opt_args.out if not has_out
result
referencedBySet: (profile, options, opt_out) ->
referencedBySet: (profile, options, opt_args) ->
profileKey = exports.nameAsKey(profile)
result = opt_out ? {}
opt_args ?= {}
has_out = opt_args.out?
result = opt_args.out ?= {}
exports.each options, (key, prof) ->
if exports.directReferenceSet(prof)[profileKey]
result[key] = prof.name
exports.referencedBySet(prof, options, result)
exports.referencedBySet(prof, options, opt_args)
delete opt_args.out if not has_out
result
validResultProfilesFor: (profile, options) ->
profile = exports.byName(profile, options)
Expand Down
12 changes: 12 additions & 0 deletions omega-pac/test/profiles.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ describe 'Profiles', ->
profile = {}
profile = Profiles.byName('profile', {"+profile": profile})
profile.should.equal(profile)
describe '#allReferenceSet', ->
profile = Profiles.create('test', 'VirtualProfile')
profile.defaultProfileName = 'bogus'
it 'should throw if referenced profile does not exist', ->
getAllReferenceSet = ->
Profiles.allReferenceSet(profile, {})
getAllReferenceSet.should.throw(Error)
it 'should process a dumb profile for each missing profile if requested', ->
profile.defaultProfileName = 'bogus'
refs = Profiles.allReferenceSet profile, {}, profileNotFound: 'dumb'
refs['+bogus'].should.equal('bogus')

describe 'SystemProfile', ->
it 'should be builtin with the name "system"', ->
profile = Profiles.byName('system')
Expand Down
19 changes: 15 additions & 4 deletions omega-target/src/options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,23 @@ class Options
###
watch: (callback) -> @_storage.watch null, callback

_profileNotFound: (name) ->
@log.error("Profile #{name} not found! Things may go very, very wrong.")
return OmegaPac.Profiles.create({
name: name
profileType: 'VirtualProfile'
defaultProfileName: 'direct'
})

###*
# Get PAC script for profile.
# @param {?string|Object} profile The name of the profile, or the profile.
# @param {bool=false} compress Compress the script if true.
# @returns {string} The compiled
###
pacForProfile: (profile, compress = false) ->
ast = OmegaPac.PacGenerator.script(@_options, profile)
ast = OmegaPac.PacGenerator.script(@_options, profile,
profileNotFound: @_profileNotFound.bind(this))
if compress
ast = OmegaPac.PacGenerator.compress(ast)
Promise.resolve OmegaPac.PacGenerator.ascii(ast.print_to_string())
Expand All @@ -324,7 +333,8 @@ class Options
if not allReferenceSet?
allReferenceSet =
if profile
OmegaPac.Profiles.allReferenceSet profile, @_options
OmegaPac.Profiles.allReferenceSet(profile, @_options,
profileNotFound: @_profileNotFound.bind(this))
else
{}
if allReferenceSet[key]
Expand Down Expand Up @@ -358,7 +368,8 @@ class Options

@_currentProfileName = profile.name
@_isSystem = options?.system || (profile.profileType == 'SystemProfile')
@_watchingProfiles = OmegaPac.Profiles.allReferenceSet(profile, @_options)
@_watchingProfiles = OmegaPac.Profiles.allReferenceSet(profile, @_options,
profileNotFound: @_profileNotFound.bind(this))

@_state.set({
'currentProfileName': @_currentProfileName
Expand Down Expand Up @@ -390,7 +401,7 @@ class Options
OmegaPac.Profiles.updateRevision(@_tempProfile)

@_watchingProfiles = OmegaPac.Profiles.allReferenceSet(@_tempProfile,
@_options)
@_options, profileNotFound: @_profileNotFound.bind(this))
@applyProfileProxy(@_tempProfile, profile)
else
@applyProfileProxy(profile)
Expand Down
13 changes: 12 additions & 1 deletion omega-web/src/omega/controllers/master.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ angular.module('omega').controller 'MasterCtrl', ($scope, $rootScope, $window,
return unless profileName
profile = $rootScope.profileByName(profileName)
return if profile.profileType in ['DirectProfile', 'SystemProfile']
ast = OmegaPac.PacGenerator.script($rootScope.options, profileName)
missingProfile = null
profileNotFound = (name) ->
missingProfile = name
return 'dumb'
ast = OmegaPac.PacGenerator.script($rootScope.options, profileName,
profileNotFound: profileNotFound)
pac = ast.print_to_string(beautify: true, comments: true)
pac = OmegaPac.PacGenerator.ascii(pac)
blob = new Blob [pac], {type: "text/plain;charset=utf-8"}
fileName = profileName.replace(/\W+/g, '_')
saveAs(blob, "OmegaProfile_#{fileName}.pac")
if missingProfile
$timeout ->
$rootScope.showAlert(
type: 'error'
message: tr('options_profileNotFound', [missingProfile])
)

diff = jsondiffpatch.create(
objectHash: (obj) -> JSON.stringify(obj)
Expand Down

0 comments on commit 6239826

Please sign in to comment.